locked
Chineese characters in output RRS feed

  • Question

  • My PowerScript script contains a piece of code like:

    $logfile = "C:\myfiles\mylog.txt"

    $name = "My name is Mario"
    $name | Out-File -FilePath $logfile -Encoding ASCII -append

    $now = Get-Date -Format "yyyy.MM.dd HH:mm:ss"
    $now | Out-File -FilePath $logfile -Encoding ASCII -append

    When I open mylog.txt I see Chineese (or Japaneese) characters instead of the ASCII characters I expect.

    What am I missing?

    I'm using PowerShell 2.0 on a Windows Server 2008 R2 server with Italian local language.

    Regards

    Mario

    Monday, November 27, 2017 4:19 PM

All replies

  • please check you Keyboard language this must not be the used OS language.


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    Monday, November 27, 2017 4:31 PM
  • This is because you are appending to a Unicode file but forcing the encoding to be ASCII.

    Do it this way:

    logfile = "C:\myfiles\mylog.txt"
    
    $name = "My name is Mario"
    $name | Out-File -FilePath $logfile -append
    
    $now = Get-Date -Format "yyyy.MM.dd HH:mm:ss"
    $now | Out-File -FilePath $logfile  -append
    

    or this way:

    logfile = "C:\myfiles\mylog.txt"
    
    $name = "My name is Mario"
    $name | Out-File -FilePath $logfile
    $now = Get-Date -Format "yyyy.MM.dd HH:mm:ss"
    $now | Out-File -FilePath $logfile -append
    


    \_(ツ)_/

    Monday, November 27, 2017 7:33 PM