0

I am trying to dump a large string to a text file over the network, but I am only getting around 64kbit per second. Read a bit about different methods to write files and saw examples using Streamwriter that was quite fast, but that was writing to a local file.

Encountered this thread where the recommendation was to increase the buffer size. Does anyone know how to do this in Powershell?

Including the link for reference:

Writing to file using StreamWriter much slower than file copy over slow network

Thanks!

Edit

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($PathToFile, $LargeTextMass, $Utf8NoBomEncoding, 0x10000)

Cannot find an overload for "WriteAllLines" and the argument count: "4".

Edit 2:

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
$sw = new-object System.IO.StreamWriter "test2.txt", $false, $Utf8NoBomEncoding,0x10000
Community
  • 1
  • 1
PatrikJ
  • 2,327
  • 3
  • 24
  • 35
  • pretty much the way it is explained in the example you linked. You're creating your streamwriter with the necessary constructor parameters, in this case the stream, the encoding and the buffer size: https://msdn.microsoft.com/en-US/library/72d9f8d5(v=vs.110).aspx . Or string, bool, encoding and buffer size: https://msdn.microsoft.com/en-us/library/0wf7ab94(v=vs.110).aspx – Dirk Trilsbeek Mar 23 '17 at 15:21
  • First attempt did not work so well. Maybe I need to create it first. – PatrikJ Mar 23 '17 at 16:03
  • yes, you need to create the object with the parameters first, for instance like this: $sw = new-object System.IO.StreamWriter $path System.Text.Encoding.UTF8 $false 10000. You can create .Net objects with constructor parameters with new-object. – Dirk Trilsbeek Mar 23 '17 at 16:09
  • Good one. Please add that as an example (but change the order of the parameters as the order was a bit mixed up) and I will mark it as the right answer. – PatrikJ Mar 23 '17 at 16:15

1 Answers1

1

The buffer of a StreamWriter is set during construction, so you need to use a constructor that allows you to supply the buffer size during object creation. StreamWriter has two constructors that allow you to set the buffer size.

StreamWriter Constructor (Stream, Encoding, Int32)

StreamWriter Constructor (String, Boolean, Encoding, Int32)

To create a .Net object in Powershell with constructor parameters, you can use the new-object cmdlet with multiple parameters:

[System.String] $fileName = 'C:\test\test.txt'
[System.Boolean] $append = $false
[System.Int32] $bufferSize = 10000
[System.Text.Encoding] $encoding = [System.Text.Encoding]::UTF8
$writer = new-object System.IO.StreamWriter -ArgumentList $fileName,$append,$encoding,$bufferSize

sometimes you may get an error stating that powershell couldn't find a constructor matching the parameters you supplied. Powershell sometimes uses different data types than .Net when not explicitly defining the parameter type yourself. It can therefore make sense to explicitly declare all parameters with the type expected by the constructor.

Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23