0

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"

6
  • Have you tried answer from this post: stackoverflow.com/questions/5596982/… ? Commented Apr 29, 2020 at 13:45
  • Thanks for the quick response. Actually I cannot manage to run these two commands inside the powershell in the bat file. Commented Apr 29, 2020 at 13:49
  • 1
    The PIPE is a special character. So you either quote your Powershell command to protect it or escape it with a caret if you are not going to use quotes. Commented Apr 29, 2020 at 13:51
  • Am I wrong or should it be PowerShell -Command {Get-Content 'ZipCode.csv' | Out-File 'ZipCode1.csv' -Encoding utf8} ... curly braces ... not parenthesis ... ;-) Commented Apr 29, 2020 at 14:05
  • @olaf the same error.. Commented Apr 29, 2020 at 14:10

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

What is %__AppDir__%?
Open up a Command Prompt window, enter Echo %__AppDir__% and take a look. It essentially always produces the path to the correct \System32 location, (regardless of whether you're running in x64 or x86 mode). You should always, wherever possible, use the full paths to locations without relying on registry or system variable settings, which can be readily modified. %__AppDir__% is a special dynamic variable, which cannot be overwritten, so even if you use Set "__AppDir__=test", if you Echo %__AppDir__% it will not output test.
That's very strange. It can't normally be listed through set in cmd, or dir env: in powershell.
…hence I can use it in cmd.exe without worrying about deliberate or accidental modificiation of my intended location. Off topic: Please note however, that because you can Set "__AppDir__=test", in cmd.exe, that having done so, you can still see that it is defined using Set __AppDir__. You could therefore retrieve the value of it like this For /F "Tokens=1,* Delims==" %I In ('Set __AppDir__ 2^> NUL') Do @Echo %J.

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.