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
  48. December 9th, 2019 at 22:22 | #48
  49. December 9th, 2019 at 22:26 | #49
  50. December 9th, 2019 at 22:32 | #50
  51. December 9th, 2019 at 22:39 | #51
  52. December 9th, 2019 at 22:40 | #52
  53. December 9th, 2019 at 22:42 | #53
  54. December 9th, 2019 at 22:44 | #54
  55. December 9th, 2019 at 23:06 | #55
  56. December 9th, 2019 at 23:12 | #56
  57. December 9th, 2019 at 23:13 | #57
  58. December 9th, 2019 at 23:13 | #58
  59. December 9th, 2019 at 23:20 | #59
  60. December 9th, 2019 at 23:29 | #60
  61. December 9th, 2019 at 23:34 | #61
  62. December 9th, 2019 at 23:38 | #62
  63. December 9th, 2019 at 23:40 | #63
  64. December 9th, 2019 at 23:44 | #64
  65. December 9th, 2019 at 23:45 | #65
  66. December 9th, 2019 at 23:47 | #66
  67. December 9th, 2019 at 23:50 | #67
  68. December 10th, 2019 at 08:50 | #68
  69. December 10th, 2019 at 17:55 | #69
  70. December 10th, 2019 at 18:31 | #70
  71. December 11th, 2019 at 10:08 | #71
  72. December 26th, 2019 at 13:30 | #72
  73. December 31st, 2019 at 04:40 | #73
  74. December 31st, 2019 at 06:24 | #74
  75. December 31st, 2019 at 06:28 | #75
  76. December 31st, 2019 at 06:33 | #76
  77. December 31st, 2019 at 07:02 | #77
  78. January 1st, 2020 at 01:32 | #78
  79. January 3rd, 2020 at 20:53 | #79
  80. January 4th, 2020 at 01:41 | #80
  81. January 4th, 2020 at 08:28 | #81
  82. January 7th, 2020 at 08:03 | #82
  83. January 8th, 2020 at 04:09 | #83
  84. January 8th, 2020 at 08:54 | #84
  85. January 8th, 2020 at 14:24 | #85
  86. January 9th, 2020 at 01:06 | #86
  87. January 13th, 2020 at 10:07 | #87
  88. January 13th, 2020 at 18:52 | #88
  89. January 14th, 2020 at 09:33 | #89
  90. January 16th, 2020 at 17:37 | #90
  91. January 16th, 2020 at 17:51 | #91
  92. January 19th, 2020 at 14:10 | #92
  93. January 19th, 2020 at 15:14 | #93
  94. January 19th, 2020 at 19:50 | #94
  95. January 20th, 2020 at 17:45 | #95
  96. January 20th, 2020 at 20:47 | #96
  97. January 21st, 2020 at 10:01 | #97
  98. January 22nd, 2020 at 00:26 | #98
  99. January 22nd, 2020 at 10:03 | #99
  100. January 22nd, 2020 at 23:47 | #100
  101. January 23rd, 2020 at 10:27 | #101
  102. January 23rd, 2020 at 21:08 | #102
  103. January 24th, 2020 at 14:16 | #103
  104. January 25th, 2020 at 12:19 | #104
  105. January 27th, 2020 at 05:10 | #105
  106. January 28th, 2020 at 00:59 | #106
  107. January 28th, 2020 at 14:37 | #107
  108. January 28th, 2020 at 23:38 | #108
  109. January 30th, 2020 at 10:17 | #109
  110. January 31st, 2020 at 21:58 | #110
  111. February 1st, 2020 at 21:10 | #111
  112. February 3rd, 2020 at 10:13 | #112
  113. February 3rd, 2020 at 18:06 | #113
  114. February 4th, 2020 at 12:51 | #114
  115. February 6th, 2020 at 14:41 | #115
  116. February 9th, 2020 at 14:04 | #116
  117. February 9th, 2020 at 15:01 | #117
  118. February 9th, 2020 at 17:38 | #118
  119. February 10th, 2020 at 17:48 | #119
  120. February 10th, 2020 at 21:46 | #120
  121. February 11th, 2020 at 21:59 | #121
  122. February 12th, 2020 at 03:02 | #122
  123. February 15th, 2020 at 03:28 | #123
  124. February 15th, 2020 at 05:39 | #124
  125. February 16th, 2020 at 20:09 | #125
  126. February 17th, 2020 at 07:36 | #126
  127. February 20th, 2020 at 23:45 | #127
  128. February 21st, 2020 at 23:20 | #128
  129. February 23rd, 2020 at 09:50 | #129
  130. February 26th, 2020 at 10:37 | #130
  131. February 26th, 2020 at 17:56 | #131
  132. February 26th, 2020 at 21:51 | #132
  133. February 27th, 2020 at 01:58 | #133
  134. February 27th, 2020 at 21:58 | #134
  135. February 27th, 2020 at 22:20 | #135
  136. February 28th, 2020 at 02:45 | #136
  137. February 28th, 2020 at 11:21 | #137
  138. February 28th, 2020 at 17:51 | #138
  139. February 28th, 2020 at 19:11 | #139
  140. February 29th, 2020 at 01:04 | #140
  141. February 29th, 2020 at 05:26 | #141
  142. February 29th, 2020 at 05:46 | #142
  143. February 29th, 2020 at 08:20 | #143
  144. February 29th, 2020 at 08:35 | #144
  145. March 1st, 2020 at 02:05 | #145
  146. March 1st, 2020 at 10:50 | #146
  147. March 3rd, 2020 at 05:27 | #147
  148. March 3rd, 2020 at 15:26 | #148
  149. March 4th, 2020 at 22:11 | #149
  150. March 5th, 2020 at 21:33 | #150
  151. March 6th, 2020 at 22:09 | #151
  152. March 8th, 2020 at 00:18 | #152
  153. March 8th, 2020 at 02:11 | #153
  154. March 8th, 2020 at 04:14 | #154
  155. March 8th, 2020 at 15:26 | #155
  156. March 8th, 2020 at 23:34 | #156
  157. March 9th, 2020 at 03:44 | #157
  158. March 11th, 2020 at 00:32 | #158
  159. March 13th, 2020 at 06:20 | #159
  160. March 14th, 2020 at 09:44 | #160
  161. March 16th, 2020 at 19:30 | #161
  162. March 21st, 2020 at 03:40 | #162
  163. March 21st, 2020 at 04:00 | #163
  164. March 21st, 2020 at 04:06 | #164
  165. March 24th, 2020 at 03:11 | #165
  166. March 24th, 2020 at 07:13 | #166
  167. March 25th, 2020 at 07:25 | #167
  168. March 25th, 2020 at 13:48 | #168
  169. March 26th, 2020 at 00:40 | #169
  170. March 26th, 2020 at 04:07 | #170
  171. April 1st, 2020 at 18:54 | #171
  172. April 1st, 2020 at 23:43 | #172
  173. April 2nd, 2020 at 06:24 | #173
  174. April 3rd, 2020 at 09:06 | #174
  175. April 4th, 2020 at 03:07 | #175
  176. April 5th, 2020 at 16:39 | #176
  177. April 6th, 2020 at 04:13 | #177
  178. April 6th, 2020 at 12:23 | #178
  179. April 6th, 2020 at 12:33 | #179
  180. April 6th, 2020 at 12:46 | #180
  181. April 6th, 2020 at 13:03 | #181
  182. April 6th, 2020 at 13:06 | #182
  183. April 6th, 2020 at 13:15 | #183
  184. April 6th, 2020 at 13:26 | #184
  185. April 6th, 2020 at 13:37 | #185
  186. April 6th, 2020 at 13:47 | #186
  187. April 6th, 2020 at 13:55 | #187
  188. April 6th, 2020 at 14:27 | #188
  189. April 6th, 2020 at 14:43 | #189
  190. April 6th, 2020 at 14:44 | #190
  191. April 6th, 2020 at 14:59 | #191
  192. April 7th, 2020 at 10:12 | #192
  193. April 7th, 2020 at 14:14 | #193
  194. April 8th, 2020 at 04:08 | #194
  195. April 8th, 2020 at 07:08 | #195
  196. April 8th, 2020 at 11:15 | #196
  197. April 8th, 2020 at 13:41 | #197
  198. April 9th, 2020 at 04:23 | #198
  199. April 9th, 2020 at 10:55 | #199
  200. April 9th, 2020 at 13:13 | #200
  201. April 10th, 2020 at 00:48 | #201
  202. April 10th, 2020 at 04:22 | #202
  203. April 10th, 2020 at 08:06 | #203
  204. April 12th, 2020 at 00:25 | #204
  205. April 12th, 2020 at 00:32 | #205
  206. April 12th, 2020 at 00:40 | #206
  207. April 12th, 2020 at 00:44 | #207
  208. April 12th, 2020 at 00:48 | #208
  209. April 12th, 2020 at 00:56 | #209
  210. April 12th, 2020 at 01:03 | #210
  211. April 12th, 2020 at 01:18 | #211
  212. April 12th, 2020 at 01:34 | #212
  213. April 12th, 2020 at 01:39 | #213
  214. April 12th, 2020 at 01:57 | #214
  215. April 12th, 2020 at 02:28 | #215
  216. April 12th, 2020 at 02:35 | #216
  217. April 12th, 2020 at 04:10 | #217
  218. April 12th, 2020 at 10:43 | #218
  219. April 12th, 2020 at 14:58 | #219
  220. April 12th, 2020 at 17:18 | #220
  221. April 13th, 2020 at 02:32 | #221
  222. April 15th, 2020 at 11:20 | #222
  223. April 16th, 2020 at 04:45 | #223
  224. April 17th, 2020 at 13:47 | #224
  225. April 21st, 2020 at 02:12 | #225
  226. April 21st, 2020 at 02:32 | #226
  227. April 21st, 2020 at 02:47 | #227
  228. April 21st, 2020 at 02:48 | #228
  229. April 21st, 2020 at 03:00 | #229
  230. April 21st, 2020 at 03:04 | #230
  231. April 21st, 2020 at 03:08 | #231
  232. April 21st, 2020 at 03:21 | #232
  233. April 21st, 2020 at 03:22 | #233
  234. April 21st, 2020 at 03:31 | #234
  235. April 21st, 2020 at 03:39 | #235
  236. April 21st, 2020 at 03:44 | #236
  237. April 21st, 2020 at 03:57 | #237
  238. April 21st, 2020 at 04:11 | #238
  239. April 22nd, 2020 at 17:08 | #239
  240. April 23rd, 2020 at 21:15 | #240
  241. April 24th, 2020 at 15:17 | #241
  242. April 24th, 2020 at 15:45 | #242
  243. April 26th, 2020 at 12:31 | #243
  244. April 27th, 2020 at 03:31 | #244
  245. April 27th, 2020 at 09:45 | #245
  246. April 27th, 2020 at 15:56 | #246
  247. April 28th, 2020 at 12:16 | #247
  248. April 29th, 2020 at 02:06 | #248
  249. April 29th, 2020 at 03:55 | #249
  250. April 29th, 2020 at 08:52 | #250
  251. April 30th, 2020 at 01:59 | #251
  252. April 30th, 2020 at 06:39 | #252
  253. April 30th, 2020 at 11:12 | #253
  254. May 1st, 2020 at 11:56 | #254
  255. May 1st, 2020 at 12:33 | #255
  256. May 1st, 2020 at 12:36 | #256
  257. May 1st, 2020 at 12:43 | #257
  258. May 1st, 2020 at 12:46 | #258
  259. May 1st, 2020 at 12:49 | #259
  260. May 1st, 2020 at 13:00 | #260
  261. May 1st, 2020 at 13:26 | #261
  262. May 1st, 2020 at 13:49 | #262
  263. May 1st, 2020 at 13:50 | #263
  264. May 1st, 2020 at 14:05 | #264
  265. May 1st, 2020 at 14:16 | #265
  266. May 1st, 2020 at 14:25 | #266
  267. May 1st, 2020 at 15:02 | #267
  268. May 7th, 2020 at 00:34 | #268
  269. May 10th, 2020 at 21:32 | #269
  270. May 10th, 2020 at 21:33 | #270
  271. May 10th, 2020 at 21:52 | #271
  272. May 10th, 2020 at 21:52 | #272
  273. May 10th, 2020 at 22:04 | #273
  274. May 10th, 2020 at 22:12 | #274
  275. May 10th, 2020 at 22:19 | #275
  276. May 10th, 2020 at 22:25 | #276
  277. May 10th, 2020 at 22:25 | #277
  278. May 10th, 2020 at 22:32 | #278
  279. May 10th, 2020 at 22:40 | #279
  280. May 10th, 2020 at 23:04 | #280
  281. May 10th, 2020 at 23:12 | #281
  282. May 10th, 2020 at 23:12 | #282
  283. May 10th, 2020 at 23:41 | #283
  284. May 11th, 2020 at 07:10 | #284
  285. May 14th, 2020 at 01:15 | #285
  286. May 15th, 2020 at 21:05 | #286
  287. May 15th, 2020 at 21:30 | #287
  288. May 15th, 2020 at 21:50 | #288
  289. May 15th, 2020 at 21:50 | #289
  290. May 15th, 2020 at 22:48 | #290
  291. May 16th, 2020 at 00:35 | #291
  292. May 16th, 2020 at 03:21 | #292
  293. May 16th, 2020 at 08:47 | #293
  294. May 16th, 2020 at 10:27 | #294
  295. May 16th, 2020 at 13:49 | #295
  296. May 16th, 2020 at 13:59 | #296
  297. May 16th, 2020 at 15:09 | #297
  298. May 17th, 2020 at 02:54 | #298
  299. May 17th, 2020 at 06:37 | #299
  300. May 18th, 2020 at 00:53 | #300
  301. May 18th, 2020 at 03:46 | #301
  302. May 18th, 2020 at 12:27 | #302
  303. May 23rd, 2020 at 02:22 | #303
  304. May 23rd, 2020 at 03:34 | #304
  305. May 23rd, 2020 at 03:38 | #305
  306. May 23rd, 2020 at 03:42 | #306
  307. May 23rd, 2020 at 04:05 | #307
  308. May 23rd, 2020 at 04:21 | #308
  309. May 23rd, 2020 at 05:21 | #309
  310. May 24th, 2020 at 19:48 | #310
  311. May 25th, 2020 at 02:47 | #311
  312. May 25th, 2020 at 21:49 | #312
  313. May 26th, 2020 at 21:16 | #313
  314. May 26th, 2020 at 21:21 | #314
  315. May 26th, 2020 at 21:25 | #315
  316. May 26th, 2020 at 21:43 | #316
  317. May 26th, 2020 at 22:54 | #317
  318. May 26th, 2020 at 23:46 | #318
  319. May 27th, 2020 at 14:10 | #319
  320. May 28th, 2020 at 07:42 | #320
  321. May 28th, 2020 at 20:29 | #321
  322. June 4th, 2020 at 21:50 | #322
  323. June 4th, 2020 at 22:05 | #323
  324. June 4th, 2020 at 22:18 | #324
  325. June 4th, 2020 at 22:31 | #325
  326. June 4th, 2020 at 22:39 | #326
  327. June 4th, 2020 at 22:41 | #327
  328. June 4th, 2020 at 23:04 | #328
  329. June 4th, 2020 at 23:04 | #329
  330. June 4th, 2020 at 23:10 | #330
  331. June 4th, 2020 at 23:36 | #331
  332. June 4th, 2020 at 23:56 | #332
  333. June 5th, 2020 at 00:02 | #333
  334. June 5th, 2020 at 00:47 | #334
  335. June 8th, 2020 at 09:24 | #335
  336. June 9th, 2020 at 00:18 | #336
  337. June 9th, 2020 at 22:22 | #337
  338. June 10th, 2020 at 07:22 | #338
  339. June 11th, 2020 at 09:00 | #339
  340. June 15th, 2020 at 13:43 | #340
  341. June 18th, 2020 at 03:07 | #341
  342. June 18th, 2020 at 06:05 | #342
  343. June 18th, 2020 at 19:06 | #343
  344. June 21st, 2020 at 23:55 | #344
  345. June 22nd, 2020 at 10:39 | #345
  346. June 25th, 2020 at 08:37 | #346
  347. June 26th, 2020 at 07:55 | #347
  348. June 30th, 2020 at 16:24 | #348
  349. July 4th, 2020 at 01:24 | #349
  350. July 16th, 2020 at 00:21 | #350
  351. July 18th, 2020 at 19:41 | #351
  352. July 22nd, 2020 at 16:34 | #352
  353. July 22nd, 2020 at 16:56 | #353
  354. July 22nd, 2020 at 18:47 | #354
  355. July 22nd, 2020 at 20:15 | #355
  356. July 22nd, 2020 at 20:53 | #356
  357. July 23rd, 2020 at 02:25 | #357
  358. July 23rd, 2020 at 04:02 | #358
  359. July 23rd, 2020 at 06:05 | #359
  360. July 23rd, 2020 at 11:02 | #360
  361. July 23rd, 2020 at 11:38 | #361
  362. July 23rd, 2020 at 13:23 | #362
  363. July 24th, 2020 at 11:49 | #363
  364. July 25th, 2020 at 04:10 | #364
  365. July 27th, 2020 at 09:14 | #365
  366. July 28th, 2020 at 01:08 | #366
  367. July 28th, 2020 at 01:25 | #367
  368. July 31st, 2020 at 01:15 | #368
  369. August 4th, 2020 at 04:48 | #369
  370. August 5th, 2020 at 14:17 | #370
  371. August 10th, 2020 at 20:01 | #371
  372. August 12th, 2020 at 21:40 | #372
  373. August 24th, 2020 at 22:05 | #373
  374. October 8th, 2020 at 07:38 | #374
  375. October 9th, 2020 at 13:36 | #375
  376. November 20th, 2020 at 18:26 | #376