shawmanz32na shawmanz32na - 4 months ago 16
Powershell Question

Here-String prints extra lines when piped to Out-File

I'm trying to programattically create a file using Powershell (I know, noob stuff) with multiple lines of content.

My attempt is as follows:

$ServicesYaml = @'
parameters:
twig.config:
debug: true
auto-reload: true
cache: false
'@
$ServicesYaml | Out-File -FilePath .\services.yml


This seems fine when printing to the console using
Write-Output
:
Here-String Console Output

But prints out extra lines when sending to a file: (Viewed using Atom):
enter image description here

What am I doing wrong? I obviously don't want all of the unprintable and extra whitespace that appears to be getting printed.

Answer

This is a clash of linuxy nature of GIT and Windows text file byte order mark. The Out-File commandlet uses a Windows encoding (USC-2 LE BOM) that GIT does not supports and GIT treats the file as binary instead of text. Try using Set-Content commandlet which uses UTF-8 without BOM

$ServicesYaml = '
parameters:
    twig.config:
    debug: true
    auto-reload: true
    cache: false
'
$ServicesYaml | Set-Content -FilePath .\services.yml
Comments