encoding problem

Welcome Forums General PowerShell Q&A encoding problem

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #101993

    Participant
    Points: 0
    Rank: Member

    I'm trying to run a script that analyzes some log files, and the logs are in Dansih. I can run the script inline (as in entering the lines one by one to the terminal) without a problem, but when I try to run the script as a file (.\script.ps1 ARGS), the encoding breaks and dose not process the danish charachters. I have tried many thing Including

     $OutputEncoding = [System.Text.Encoding]::UTF8 
    $PSDefaultParameterValues = @{ '*:Encoding' = 'utf8' } 

    and have appended

     -Encoding UTF8 

    every where I can, but still no result, and the strange part is if I run the script line by line there is no problem.
    for example

    $targets = 'Kørselsdato', 'Tidspunkt', 'JobId', 'Antal læste på tabellen AFSENDNEMSMS', 'Køretid i minutter'
    $targets | Out-File ".res.txt"
    

    this will work line by line but not when I run it as a script file

    result of runing as a script :

    Kørselsdato
    Tidspunkt
    JobId
    Antal læste på tabellen AFSENDNEMSMS
    Køretid i minutter
    

    result of runing line by line :

    Kørselsdato
    Tidspunkt
    JobId
    Antal læste på tabellen AFSENDNEMSMS
    Køretid i minutter
    
  • #101998

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    Didn't have a problem with this as a .ps1

    $targets = 'Kørselsdato', 'Tidspunkt', 'JobId', 'Antal læste på tabellen AFSENDNEMSMS', 'Køretid i minutter'
    $targets | Out-File C:\tmp\test.txt
    

    If you open your text file in notepad and do a SaveAs, what does it say next to the save button what the encoding is?

    • #101999

      Participant
      Points: 0
      Rank: Member

      UTF8 Signature and I also used this

       Out-File -Encoding utf8 "res.txt" 
    • #102002

      Participant
      Points: 190
      Helping Hand
      Rank: Participant

      If you skip the out-file and just output the $targets variable to the console when you run it as a script.
      Does it output the content correctly or is it scrambled there as well?

    • #102004

      Participant
      Points: 0
      Rank: Member

      no, The same problem

  • #102005

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    What is the output if you type/run:

    $OutputEncoding
    [console]::OutputEncoding
    

    Before you run the script?

The topic ‘encoding problem’ is closed to new replies.