top of page

PowerShell script to add text to a file

You can easily add text to the end of a text file using the PowerShell Add-Content command. So if you have text file like this which has only one line of text:



. . .and you want to add four new lines of text, you can write a PowerShell script like this:


Add-Content "C:\foofolder\test\test\NL East.txt" "`nPhiladelphia Phillies"

Add-Content "C:\foofolder\test\test\NL East.txt" "Atlanta Braves"

Add-Content "C:\foofolder\test\test\NL East.txt" "Washington Nationals"

Add-Content "C:\foofolder\test\test\NL East.txt" "Florida Marlins"


Open Windows PowerShell ISE [everyone running Windows should have this preinstalled], and enter the script in the script pane, and then run the script by clicking the green play button.


Note that for the first new line, we need to indicate that a line break should be entered. This is done by using a backtick '`' and an 'n' as a reference. The backtick is not the same as an apostrophe. On most keyboards it will be at the top far left before the number keys, and below the tilde key. Adding a `n reference for each new line will lead to the new entries being double spaced.



Comments


bottom of page