I have no experience on creating Power Shell scripts and I’m trying to instantiating System.IO.StreamWriter class providing a specific text encoding. I’m trying to do this:
$file = New-Object System.IO.StreamWriter("sample.txt", [System.Text.Encoding]::Default, 4096)
And I’m getting the following error message:
New-Object : Cannot find an overload for "StreamWriter" and the argument count: "3".
At line:1 char:18
+ $out = New-Object <<<< System.IO.StreamWriter("sample.txt", [System.Text.Encoding]::Default, 4096)
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I looked at MSDN documentation StreamWriter Constructor (Stream, Encoding, Int32) and it has an overload with three parameters.
Does anyone know how to instantiate a System.IO.StreamWriter specifiying an encoding?
Thanks,
You’re trying to call
StreamWriter(String, Encoding, Int32)– and that signature doesn’t exist. Admittedly the error message leaves a little to be desired… but basically you’ll either need to create the stream yourself, or usingStreamWriter(String, Boolean, Encoding).