4

I'm using powershell to create a file, I need this file to be UTF8 Encoded, thus far all of the attempts I've tried have failed.

Inspecting the file in Notepad++ shows UCS-2 LE BOM as the encoding. Is it possible for powershell to use UTF8 instead?

So far I've tried -encoding utf8 and currently I'm using [IO.File]::WriteAllLines($filename, $text)

There may be an underlying issue (forgive me, very new to Powershell) that is causing the problem as I receive this error in the console however the file is being created:

    Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.
    + CategoryInfo          : InvalidArgument: (:) [Out-File], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.OutFileCommand
    + PSComputerName        : 50.19.209.240

ChangeFileModeByMask error (3): The system cannot find the path specified.
    + CategoryInfo          : NotSpecified: (ChangeFileModeB...path specified.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : 50.19.209.240

Edit:

Edited after answer given to provide more info.

Details from the file:

write-host "Creating Application.conf in UTF-8"
$filename = "c:\application.conf"
[IO.File]::WriteAllLines($filename, $text, [System.Text.Encoding]::UTF8) 

the output in the console is still erroring as per the above.

2 Answers 2

4

You need to use different overload of WriteAllLines: File.WriteAllLines Method (String, String[], Encoding), see here: https://msdn.microsoft.com/en-us/library/3det53xh(v=vs.110).aspx

It will be:

[IO.File]::WriteAllLines($filename, $text, [System.Text.Encoding]::UTF8)

And of course you can use PS way:

$text | Out-File $filename -encoding Utf8
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Andrey, I've tried both of the suggestions, the latter was my original attempt and the former is what I've just tried. I still receive the error in console and it still creates it in the encoding that's not desired. Would you mind looking at the edited post and seeing if there's an obvious mistake please?
right of course, the segment '$text = "asdaasdad" $filename = "c:\GAAAAAH.conf" [IO.File]::WriteAllLines($filename, $text, [System.Text.Encoding]::UTF8) ' by itself works fine.
it's inside a function which is called from within the file if that helps.
Well no, but the filename variable is set the line before it is used, which is contained within the same function so I'm not sure how that can be null? $filename = "c:\cfn\jars\CustomerStruggleStreaming\application.conf" if literally the line before as per the edit
That's the odd thing, the path is valid and the file is created. I don't understand how it can be null and then use it when the file is saved
3

Any special reason, why you use .net classes? You can also use set-content cmdlet.

"text" | Set-Content -Encoding UTF8 -Path "c:\path\file.txt"

3 Comments

no reason other than not knowing any different really. :)
not currently, I picked up this script and was tasked with making some changes. Could you take a peek at the edited question please?
This still prepends a Byte Order Mark, which WriteAllLines doesn't, though the latter need the full path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.