Dimitris Markopoulos
Dimitris Markopoulos

Reputation: 21

Syntax error in powershell command in windows

I try to run two commands in a bat file using the powershell. My goal is to transform a file to a utf8 format. How can I achieve that?

Here is what I have so far:

PowerShell -Command (Get-Content 'ZipCode.csv' | Out-File 'ZipCode1.csv' -Encoding utf8)

I get the following error: "out-file is not recognized as an internal or external command"

Upvotes: 0

Views: 513

Answers (2)

Compo
Compo

Reputation: 38719

If you're only using Out-File because your version of PowerShell doesn't include the -Encoding option with Set-Content, then it should read:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Content -Path '.\ZipCode.csv' | Out-File -FilePath '.\ZipCode1.csv' -Encoding UTF8"

Obviously if you have a Version of PowerShell where Set-Content has the required -Encoding option, use it instead:

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Content -LiteralPath 'ZipCode.csv' | Set-Content -LiteralPath 'ZipCode1.csv' -Encoding UTF8"


These could obviously be shortened to remove the robustness and use aliases/shorthand:

@PowerShell -NoP "GC '.\ZipCode.csv'|Out-File '.\ZipCode1.csv' -E UTF8"
@PowerShell -NoP "GC -LP 'ZipCode.csv'|SC -LP 'ZipCode1.csv' -En UTF8"

I prefer to use -LiteralPath because I have a tendency to use [] in my file naming, and those can be problematic in filenames. Change the output file name to ZipCode[1], then try the -Set-Content version code with -Path or nothing instead of -LiteralPath/-LP option, and you should see what I mean.

Upvotes: 1

js2010
js2010

Reputation: 27606

The doublequotes seem sufficient to escape the pipe. Single quotes on the outside wouldn't work.

PowerShell "Get-Content ZipCode.csv | Out-File ZipCode1.csv -Encoding utf8"

Upvotes: 2

Related Questions