The PowerShell Add-Content cmdlet appends text to a file. Use it when you want to keep existing lines and add more — the opposite of Set-Content, which replaces the whole file.
For the official reference, see Microsoft Learn’s Add-Content.
Want to practice PowerShell in a live browser terminal (no install)? Try the free interactive lessons on CMD Master — PowerShell file and text commands.
Append a line
Add-Content -Path .\log.txt -Value "Started at $(Get-Date -Format o)"
If log.txt does not exist yet, PowerShell creates it. If it already has lines, your new value is written after them.
Append several lines
Pass multiple strings and each becomes its own line:
Add-Content -Path .\notes.txt -Value "Todo: backup", "Todo: test deploy", "Todo: ping ops"
Or build an array first:
$entries = @(
"Build finished",
"Tests passed",
"Artifact uploaded"
)
Add-Content -Path .\build.log -Value $entries
Append vs replace
Remember the pair:
# wipe and rewrite the file
Set-Content -Path .\status.txt -Value "Fresh start"
# keep existing lines, add more
Add-Content -Path .\status.txt -Value "Still running"
Set-Content means “make the file equal this.” Add-Content means “append.” Mixing them up is how logs get accidentally wiped.
Pipe output into a growing file
Any string pipeline works:
Get-ChildItem -Name *.log | Add-Content -Path .\inventory.txt
"Heartbeat $(Get-Date -Format 'HH:mm:ss')" | Add-Content -Path .\health.log
That pattern is ideal for inventories, heartbeat files, and append-only audit trails.
Encoding when tools care
Be explicit when other tools expect UTF-8:
Add-Content -Path .\events.jsonl -Value '{"ok":true}' -Encoding utf8
Use -Encoding utf8 (or utf8BOM / unicode when a consumer needs those) so appended bytes match the rest of the file.
Create the folder first if needed
Add-Content writes the file. Create a missing parent folder before the first append:
New-Item -ItemType Directory -Path .\logs -Force | Out-Null
Add-Content -Path .\logs\app.log -Value "Boot"
Safe append habit
For important files, confirm the path before you grow it in a loop:
$path = ".\prod-notes.txt"
if (-not (Test-Path $path)) {
Set-Content -Path $path -Value "# Prod notes" -Encoding utf8
}
Add-Content -Path $path -Value "$(Get-Date -Format o) — deploy ok"
That keeps the first write intentional and every later write additive.
Quick checklist
| Goal | Cmdlet |
|---|---|
| Append lines | Add-Content |
| Replace / create file text | Set-Content |
| Read file text | Get-Content |
| Copy the file itself | Copy-Item |
Practice it live
CMD Master is a free online interactive learning platform for the command line: practice in a live browser terminal with instant feedback, no install or VM. It covers Windows CMD, PowerShell, and Bash.
Start here: CMD Master — interactive PowerShell practice.
Top comments (0)