Author Posts

June 7, 2018 at 8:27 am

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

June 7, 2018 at 10:35 am

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?

June 7, 2018 at 10:56 am

UTF8 Signature and I also used this

 Out-File -Encoding utf8 "res.txt" 

June 7, 2018 at 11:10 am

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?

June 7, 2018 at 11:11 am

no, The same problem

June 7, 2018 at 11:37 am

What is the output if you type/run:

$OutputEncoding
[console]::OutputEncoding

Before you run the script?