Appending text files without new line

by Apr 21, 2011

You can use Out-File with -Append to append lines to a text file, but there is no way to add text information without a line break. To do that, you can use .NET Framework directly. Here is an example:

PS > "New Line1" | Out-File $env:temp\file.txt
PS > "New Line2" | Out-File $env:temp\file.txt -Append
PS > "New Line3" | Out-File $env:temp\file.txt -Append
PS > [System.IO.File]::AppendAllText("$env:temp\file.txt", 'New Word 1', [System.Text.Encoding]::Unicode)
PS > [System.IO.File]::AppendAllText("$env:temp\file.txt", 'New Word 2', [System.Text.Encoding]::Unicode)
PS > [System.IO.File]::AppendAllText("$env:temp\file.txt", 'New Word 3', [System.Text.Encoding]::Unicode)
PS > Get-Content $env:temp\file.txt

 

New Line1
New Line2
New Line3
New Word 1New Word 2New Word 3

 

Twitter This Tip!
ReTweet this Tip!