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.

null
  • 3,469
  • 7
  • 41
  • 90

2 Answers2

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
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • 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? – null May 31 '16 at 12:39
  • right of course, the segment '$text = "asdaasdad" $filename = "c:\GAAAAAH.conf" [IO.File]::WriteAllLines($filename, $text, [System.Text.Encoding]::UTF8) ' by itself works fine. – null May 31 '16 at 12:50
  • it's inside a function which is called from within the file if that helps. – null May 31 '16 at 12:50
  • 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 – null May 31 '16 at 12:57
  • 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 – null May 31 '16 at 13:12
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"
Martin
  • 1,853
  • 3
  • 15
  • 22
  • no reason other than not knowing any different really. :) – null May 31 '16 at 12:16
  • not currently, I picked up this script and was tasked with making some changes. Could you take a peek at the edited question please? – null May 31 '16 at 12:40
  • 1
    This still prepends a Byte Order Mark, which `WriteAllLines` doesn't, though the latter need the full path. – joharr Sep 28 '16 at 20:17