Home/Powershell demo/How to Append Data to a Text File Using PowerShell

How to Append Data to a Text File Using PowerShell

 Here’s we can discuss on how to add text to the end of a text file using the Add-Content cmdlet using PowerShell.  In our example let’s add “This is the last line” to the end of a file using add-content cmdlet to append data to a text file.

The above example above adds the text to the last line of the text, it doesn’t actually create a new line. We can use an escape character to tell PowerShell to add a carriage return, new line… while appending the text in existing file.

Escape characters:

‘n — New line
‘t — Horizontal tab
‘’ — Single quote
‘” — Double quote
‘0 — Null
‘a — Alert
‘b — Backspace
‘r — Carriage return

Append Text using Escape characters:

The above example append the “This is the last line” as a new line at end of the text file with help of ‘n and’ r

 

 OUTPUT:

 

Leave a Reply