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