It’s easy to append text to a file with Windows Powershell with the Add-Content cmdlet, here are some examples:
- Create a sample text file using notepad. I have created mine like this:
- Open a Powershell Window and type:
Add-Content C:\temp\test.txt "Test"
The above example is fine, but as you can see the data was not written to a new line.
Example PowerShell to append text to a file on to a new line
To append content on to a new line you need to use the escape character followed by the letter “n”:
So to continue the example above you could use:
Add-Content C:\temp\test.txt "`nThis is a new line"
And then the resulting text file would look like this:
Append formatted data with Powershell by using tabs and new lines
This example creates a tab formatted file, the output looks like this:
The Powershell to create the above example is:
Add-Content C:\temp\test.txt "`nItem`tQty`tValue`tTotal" Add-Content C:\temp\test.txt "`nPants`t4`t32.22`t128.88" Add-Content C:\temp\test.txt "`nSocks`t3`t5.07`t15.21" Add-Content C:\temp\test.txt "`nShoes`t12`t136.57`t1638.84"
As you can see, the tabs are added with the special character “`t”.