none
How to write extended ASCII characters to a file

    Question

  • Here is a distilled fragment from some larger script. Its purpose is to create a text file containing some characters from the extended ASCII character set.

    $String = "Test" + [char]190 + [char]191 + [char]192
    echo $String | out-file d:\test.txt -Encoding ascii

    What I want in the target file is exactly the 7 characters that make up $String. The above method fails to deliver this result. How can I do it?

    Monday, April 06, 2015 8:29 AM

Answers

All replies

  • Hi,

    Try using Add-Content or Set-Content instead:

    $String = "Test" + [char]190 + [char]191 + [char]192
    echo $String | Set-Content .\test.txt


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by Frederik Long Monday, April 06, 2015 2:31 PM
    Monday, April 06, 2015 12:42 PM
  • 'Test' + [char]190 + [char]191 + [char]192 >test.txt


    \_(ツ)_/

    Monday, April 06, 2015 2:19 PM
  • Hi,

    Try using Add-Content or Set-Content instead:

    $String = "Test" + [char]190 + [char]191 + [char]192
    echo $String | Set-Content .\test.txt

    Thanks for your suggestion. It still added an unwanted CRLF to the end of the file but got me to look in the right place. Using the Set-Content method with a byte array then gave me what I needed, without the CRLF.
    Monday, April 06, 2015 2:31 PM
  • 'Test' + [char]190 + [char]191 + [char]192 >test.txt


    \_(ツ)_/


    This method gives me the extended ASCII characters but unfortunately in a Unicode file.
    Monday, April 06, 2015 2:32 PM
  • Nothing wrong with Unicode.

    If you like to change to old fashioned 20th century files type then just do this:

    'Test' + [char]190 + [char]191 + [char]192 |Out-File test.txt -Encoding ascii


    \_(ツ)_/

    • Marked as answer by Frederik Long Monday, April 06, 2015 3:11 PM
    • Unmarked as answer by Frederik Long Monday, April 06, 2015 3:11 PM
    Monday, April 06, 2015 3:02 PM
  • Nothing wrong with Unicode.

    If you like to change to old fashioned 20th century files type then just do this:

    'Test' + [char]190 + [char]191 + [char]192 |Out-File test.txt -Encoding ascii


    \_(ツ)_/

    I specifically did not want those $00 bytes that Unicode gives me. I had already tried the ASCII encoding method but found that it kills my extended ASCII bytes:

    Monday, April 06, 2015 3:14 PM
  • Why do you think that?  PowerShell cannot convert the extended set.

    Output to UTF8 or Unicode then convert the file.


    \_(ツ)_/

    Monday, April 06, 2015 3:47 PM
  • Why do you think that?  PowerShell cannot convert the extended set.

    Output to UTF8 or Unicode then convert the file.


    \_(ツ)_/

    I did not "think" that your code kills the extended ASCII bits. It is a matter of fact as demonstrated by my screen shot.

    Outputting in UTF8-format generates incorrect results too. Using Unicode would be an option but then the Set-Content method suggested by Mike can do it without further conversion.

    Monday, April 06, 2015 8:32 PM