1

I am running two Powershell scripts. One Powershell script adds the host name to a text file. Other Powershell script appends the ip address of the machine to the same file. So, the .txt file looks as follows: hostname ipaddress But, this file is being saved in Unicode format by default. What can I do so that, the text file is stored in ANSI format?

I use PowerShell v2.0.

[System.Text.Encoding]::Default 

IsSingleByte : True
BodyName : iso-8859-1 
EncodingName : Western European (Windows) 
HeaderName : Windows-1252 
WebName : Windows-1252
WindowsCodePage : 1252
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True 
CodePage : 1252 
glennsl
  • 28,186
  • 12
  • 57
  • 75
Mistletoe
  • 347
  • 2
  • 4
  • 13
  • 1
    What PowerShell version do you use? What does `[System.Text.Encoding]::Default` return? How do you write to the file? –  Jul 25 '17 at 14:39

2 Answers2

7

Depending on how you are outputting your text you may need to set the encoding type. Using Out-File -Encoding you use the type of ASCII. This also depends on the version of Powershell you're using.

See: SS64 Out-File and Set the encoding to ANSI in PowerShell 2.0

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Jason Snell
  • 1,425
  • 12
  • 22
2

The following solved my problem:

Out-File 'file.txt' -Append -Encoding Ascii

This enabled me to save the file in ANSI format.

Mistletoe
  • 347
  • 2
  • 4
  • 13