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
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.