Append Text to a File Using Add-Content in PowerShell
Here’s a quick tutorial on how to add text to the end of a text file using the Add-Content or its alias ac in PowerShell. In this first example let’s add "This is the last line" to the end of a file.
1 | Add-Content c:\sample.txt "This is the last line" |
The example above adds the text to the last line, it doesn’t actually create a new line, so let’s try it again, and make sure that this time the text that we concatenate is on the last line all by itself.
1 | Add-Content c:\sample.txt "`r`nThis is the last line" |
You’ll notice a `r`n this is an escape character to tell PowerShell to add a carriage return and a new line, you’ll need this when working with Windows file. If your going to move this file to a unix or linux computer, you’ll only need a `n there’s no need for the carriage return.
Here is a list of escape characters:
- `0 — Null
- `a — Alert
- `b — Backspace
- `n — New line
- `r — Carriage return
- `t — Horizontal tab
- `’ — Single quote
- `” — Double quote
The example I gave above was the quick and dirty, we should really explicitly state our arguments and use quotes around the path, to do that we will add a path and a value argument
1 | Add-Content -Path "c:\sample.txt" -Value "`r`nThis is the last line" |
Here’s a useful example, suppose you want to map hostname to a particular IP address. Let’s map example.com to point to 127.0.0.1, we’ll do this using the ac alias for Add-Content.
1 | ac -Path "C:\WINDOWS\system32\drivers\etc\hosts" -Value "`r`n127.0.0.1 example.com" |
Hi there! I’m at work surfing around your blog from my new iphone 3gs!
Just wanted to say I love reading through
your blog and look forward to all your posts!
Keep up the excellent work!
Hmm is anyone else encountering problems with the pictures on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the
blog. Any suggestions would be greatly appreciated.
Greetings! Very useful advice in this particular post!
It’s the little changes that make the most
important changes. Thanks a lot for sharing!
I have learn some just right stuff here. Definitely value bookmarking for revisiting.
I wonder how so much attempt you place to create one of these magnificent informative site.
Write more, thats all I have to say. Literally, it
seems as though you relied on the video to make your point.
You definitely know what youre talking about, why throw away your intelligence on just posting videos
to your site when you could be giving us something informative to read?
What’s up to all, since I am really keen of reading this website’s post to be updated regularly.
It includes pleasant stuff.
I do not know whether it’s just me or if perhaps everybody else encountering issues with your blog.
It looks like some of the text on your content are
running off the screen. Can someone else please comment and let me know if this is
happening to them as well? This might be a issue with my internet browser
because I’ve had this happen before. Many thanks
Elites and “quick for age” runners begin at the Blue Start in Blackheath. Fast and flat, but also with a lot of tight corners and slender sections, the course options a purple dashed line that athletes use to maintain from dropping their approach alongside the wending path. In many instances yow will discover motherboard and CPU combos in this price range, which is one other good way to construct an affordable machine or a reasonable home/workplace computer. Others, nevertheless, show that even bargain-basement tablets are nice after they discover the best audience. What great eating places can you find with the help of an app? Many small websites are looking without cost writing assist. See what’s on the frame – You possibly can see which footage are currently displaying on every frame in your account, in addition to which photos are waiting to be downloaded and which of them have been deleted. The first time the frame connects, it dials a toll-free quantity and downloads the settings you created from the web site. Why are there so many different image formats on the internet?