Setting the correct encoding when using Out-File

27 Jun

One caveat I realized today when trying to create a file using Out-file in powershell.

The command is simple enough:

dir c:\temp\*.exe | Out-file c:\test.txt -append

After that, I used for loop in command prompt to process the txt file

for /f %a in (c:\test.txt) do @echo %a

Guess what? Nothing happened! The above command does not work as it should!

This is due to the encoding used by Out-file. The default encoded in unicode, which the batch command could not read. So if you intend to generate a file for tools like batch commands to process, you must ensure that the encoding is correct.Using this command will make the txt file readable by the FOR command later.

dir c:\temp | Out-file c:\test.txt -append -Encoding "default"
Leave a comment

Posted by on June 27, 2012 in Scripts, VBScripts



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: