# encoding problem

**URL:** <https://forums.powershell.org/t/encoding-problem/10779>\
**Category:** PowerShell Help\
**Created:** [June 7, 2018, 2:57am UTC](https://forums.powershell.org/t/encoding-problem/10779 "2018-06-07T02:57:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakhtyar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bakhtyar](https://forums.powershell.org/u/bakhtyar)\
**Post date:** [June 7, 2018, 2:57am UTC](https://forums.powershell.org/t/encoding-problem/10779/1 "2018-06-07T02:57:58Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [June 7, 2018, 5:05am UTC](https://forums.powershell.org/t/encoding-problem/10779/2 "2018-06-07T05:05:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bakhtyar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bakhtyar](https://forums.powershell.org/u/bakhtyar)\
**Post date:** [June 7, 2018, 5:26am UTC](https://forums.powershell.org/t/encoding-problem/10779/3 "2018-06-07T05:26:02Z")

</div>

UTF8 Signature and I also used this

```
Out-File -Encoding utf8 “res.txt”
```

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [June 7, 2018, 5:40am UTC](https://forums.powershell.org/t/encoding-problem/10779/4 "2018-06-07T05:40:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bakhtyar](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bakhtyar](https://forums.powershell.org/u/bakhtyar)\
**Post date:** [June 7, 2018, 5:41am UTC](https://forums.powershell.org/t/encoding-problem/10779/5 "2018-06-07T05:41:55Z")

</div>

no, The same problem

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [June 7, 2018, 6:07am UTC](https://forums.powershell.org/t/encoding-problem/10779/6 "2018-06-07T06:07:28Z")

</div>

What is the output if you type/run:

```
$OutputEncoding
[console]::OutputEncoding
```

Before you run the script?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/encoding-problem/10779/7 "2024-05-16T20:34:57Z")

</div>


