Nico
Nico

Reputation: 251

Powershell generates .bat, and put special character

I'm currently working with powershell in order to create a .bat script.

I put text in .bat script with >>

For example,

Write "start program xxx" >> script.bat

but when i try to execute this script.bat with cmd, it says : "■s" is not recognize ... etc. And in powershell it says : 'þp' is not recognize ..

So I guess that doing >> script put special character at the beginning of the line. If someone got information on this. And what those "■s" and 'þp' are.

Upvotes: 1

Views: 1728

Answers (1)

Richard
Richard

Reputation: 108975

The file redirection operators (>> etc.) will write text encoded in UTF-16. If the file already contains text in a different encoding everything will be confused (and I'm not use of cmd.exe understands UTF-16 at all.

Easier to use Out-File with the -encoding parameter to specify something consistent. Use the -append switch parameter to append rather than overwriting.

Eg.

"Some text" | Out-File -encoding ASCII -append -FilePath 'script.bat`

(If you find yourself writing the same out-file and parameters, then put it in a helper advanced function that will read pipeline input to encapsulate the out-file.)

Upvotes: 4

Related Questions