Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1060

PowerShell 5.1, Output to a text file with Out-File / Set-Content and utf8

$
0
0

I'm a bit stumped at how to interpret this in PS 5.1:First, I try to create a file with Set-Content and Add-Content

"# Script for running installers`n`n" | Out-File $InstallTools -Encoding utf8"sleep 5" | Add-Content $InstallConsoleTools"Start-Process -WindowStyle Hidden `"powershell.exe`" -ArgumentList `"-NoProfile -ExecutionPolicy Bypass -File `"`"$SetupScript`"`"`"" | Add-Content $InstallTools

Output is a mess:

# Script for running installerssleep 5S t a r t - P r o c e s s   - W i n d o w S t y l e   H i d d e n   " p o w e r s h e l l . e x e "   - A r g u m e n t L i s t   " - N o P r o f i l e   - E x e c u t i o n P o l i c y   B y p a s s   - F i l e   " " C : \ U s e r s \ B o s s \ I n s t a l l   w i n f e t c h . p s 1 " " " 

The Set-Content and first Add-Content were fine, so it's something in that last line. I tried also with Out-File -Encoding utf8 and Out-File -Append and the result is the same.

The above output is when I open in notepad. If I open in VS Code, it's worse; I see no text after the sleep 5 just a load of nulls.

Why do the first 2 lines work fine, then the third is garbled, and how should I construct things to get meaningful output?

Edit: Believe I have resolved the problem, by adding -Encoding utf8 to the Out-File -Append lines. I read somewhere that the -Append and -Encoding switches should not be used together, but it seems they must be to get consistent output. Question can be closed unless someone has spotted a mistake in what I've found.


Viewing all articles
Browse latest Browse all 1060

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>