You can remove empty line like this:
Set-Content C:\test.txt "test",'',"test1"
Get-Content c:\test.txt | ? { $_ }
However, it will remove the string in the middle as well.
Edit: Actually as I tried the example, I noticed that Get-Content
ignores the last empty line added by Set-Content
.
I think your problem is in Set-Content
. If you use workaround with WriteAllText
, it will work fine:
[io.file]::WriteAllText('c:\test.txt', ("test",'',"test1" -join "`n"))
You pass a string as second parameter. That's why I first joined the strings via -join
and then passed it to the method.
Note: this is not recommended for large files, because of the string concatenation that is not efficient.