blank line using powershell

I want to append some text to a file and after I append the text using Set-Content, I want to also append a blank line to the file. How can I append a blank line to a file using powershell?

Either you add a space charachter or a line break / new line charachter. Please share the code you have so far.

$eline=“`r”

$text=“Testing”

$myfile=Get-Content “C:\myfile.txt”

Set-Content -Path “C:\myfile.txt” -value $text,$eline,$myfile

This is working. However is there a better way to do this?

What do you mean when you say “better way”? If it works …

I mean any alternate way to achieve this?

Probably there are several alternative ways but without knowing more of the actual task or environment and purpose I cannot see the advantage to change a working approach.

I was wondering why I cannot use the new line character \n to insert a blank line. Because when I tried to use it, it printed \n as it is in the file.

It depends on the operating system and on the editor or application how a line break / new line is treated / interpreted. If you have unix/linux files for example they look different in Windows. Usually in Windows a new line is represented by “`n`r” (new line, carriage return).

This…

\n

… is not how you do the new line. It’s this…

"`n"
'Hello world'
"`nWelcome"
 

Powershell Special Characters And Tokens – Neolisk's Tech Blog