locked
Add-Content in english? RRS feed

  • Question

  • Ok, in my first "Hello World" attempt within PowerShell, I've fun the following commands:

    Out-File -filepath "C:\temp\test.txt"
    Add-Content C:\temp\test.txt "Hello World"

    The file is created successfully, but within the text file I get Asian characters (not english anyways).  Any ideas?

     

    Tuesday, April 27, 2010 1:26 PM

Answers

  • These types of questions are probably best posted to the PowerShell forums (hosted by: Microsoft, Community) in the future, but I can help.

    What you are seeing in your example here is actually an odd default behavior of the "Out-File" cmdlet.  If you run "get-help Out-File -detail" you'll see that there is an "encoding" parameter on that cmdlet.  The parameter help indicates the following:
    ------------------------
    -Encoding <string>
     Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32",
      "ASCII", "BigEndianUnicode", "Default", and "OEM". "Unicode" is the default.
     "Default" uses the encoding of the system's current ANSI code page.
     "OEM" uses the current original equipment manufacturer code page identifier for the operating system.
    ------------------------

    So by default the cmdlet will create files using ANSI encoding.  What you want is at least ASCII, if not Unicode.  You can achieve that by either doing "Out-File -filepath "C:\temp\test.txt" -encoding ASCII", in your example above, or just by using some shortcuts and doing the following:
    ------------------------
    # Overwrite the existing file contents or create a new file if it doesn't exist
    "Hello World" > C:\temp\test2.txt

    # Append to the existing file contents or create a new file if it doesn't exist
    "Hello World" > C:\temp\test2.txt

    Wednesday, April 28, 2010 9:04 PM