Home > PowerShell > Append Text to a File Using Add-Content in PowerShell

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"
  1. No comments yet.
  1. March 5th, 2010 at 07:39 | #1
  2. November 7th, 2019 at 12:18 | #2
  3. November 7th, 2019 at 16:32 | #3
  4. November 7th, 2019 at 16:45 | #4
  5. November 7th, 2019 at 20:54 | #5
  6. November 8th, 2019 at 03:57 | #6
  7. November 8th, 2019 at 11:04 | #7
  8. November 8th, 2019 at 14:31 | #8
  9. November 8th, 2019 at 16:46 | #9
  10. November 8th, 2019 at 21:16 | #10
  11. November 8th, 2019 at 22:15 | #11
  12. November 9th, 2019 at 01:29 | #12
  13. November 9th, 2019 at 10:45 | #13
  14. November 9th, 2019 at 13:30 | #14
  15. November 9th, 2019 at 14:41 | #15
  16. November 9th, 2019 at 16:12 | #16
  17. November 9th, 2019 at 18:06 | #17
  18. November 10th, 2019 at 06:31 | #18
  19. November 11th, 2019 at 07:34 | #19
  20. November 11th, 2019 at 17:51 | #20
  21. November 11th, 2019 at 18:08 | #21
  22. November 11th, 2019 at 18:55 | #22
  23. November 11th, 2019 at 19:26 | #23
  24. November 12th, 2019 at 04:36 | #24
  25. November 12th, 2019 at 17:42 | #25
  26. November 12th, 2019 at 18:23 | #26
  27. November 12th, 2019 at 18:25 | #27
  28. November 14th, 2019 at 06:15 | #28
  29. November 14th, 2019 at 06:18 | #29
  30. November 14th, 2019 at 06:25 | #30
  31. November 14th, 2019 at 08:35 | #31
  32. November 14th, 2019 at 08:56 | #32
  33. November 14th, 2019 at 12:22 | #33
  34. November 14th, 2019 at 15:47 | #34
  35. November 14th, 2019 at 17:31 | #35
  36. November 14th, 2019 at 17:39 | #36
  37. November 14th, 2019 at 18:48 | #37
  38. November 14th, 2019 at 19:00 | #38
  39. November 14th, 2019 at 19:36 | #39
  40. November 14th, 2019 at 20:02 | #40
  41. November 14th, 2019 at 20:25 | #41
  42. November 14th, 2019 at 23:13 | #42
  43. November 15th, 2019 at 07:43 | #43
  44. November 16th, 2019 at 17:53 | #44
  45. November 20th, 2019 at 15:16 | #45
  46. November 22nd, 2019 at 16:09 | #46
  47. December 1st, 2019 at 11:19 | #47
You must be logged in to post a comment.