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.
1 |
Add-Content "C:\dotnet-helpers\DummyfiletoDelete.txt" "This is the last line" |
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:
1 |
Add-Content -Path "C:\dotnet-helpers\DummyfiletoDelete.txt" -Value "`r`nThis is the last line". |
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
1 |
Add-Content -Path "C:\dotnet-helpers\DummyfiletoDelete.txt" -Value "This is the last line added from $env:computername system" |
OUTPUT: