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"

  • Have you tried answer from this post: https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom ? – Maciek Apr 29 '20 at 13:45
  • Thanks for the quick response. Actually I cannot manage to run these two commands inside the powershell in the bat file. – Dimitris Markopoulos Apr 29 '20 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. – Squashman Apr 29 '20 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 ... ;-) – Olaf Apr 29 '20 at 14:05
  • @olaf the same error.. – Dimitris Markopoulos Apr 29 '20 at 14:10
  • From the help invoked with `Powershell /?` .. `PowerShell -Command "& {...... }"` – Olaf Apr 29 '20 at 14:45

2 Answers2

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"
js2010
  • 13,551
  • 2
  • 28
  • 40
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.

Compo
  • 30,301
  • 4
  • 20
  • 32
  • What is `%__AppDir__%`? – js2010 Apr 29 '20 at 15:28
  • 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`. – Compo Apr 29 '20 at 15:35
  • That's very strange. It can't normally be listed through `set` in cmd, or `dir env:` in powershell. – js2010 Apr 29 '20 at 15:51
  • …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`. – Compo Apr 29 '20 at 16:01