Write and append to files with Set-Content and Add-Content

The pair people forget: Set-Content overwrites, Add-Content appends — both create the file if it doesn't exist.

WindowsFilesscriptingpowershellLow risk
Use Set-Content -Path <file> -Value <text> to write, and Add-Content with the same parameters to append.

01Command

Command
$ Set-Content -Path 'C:\Temp\out.txt' -Value 'first line' ; Add-Content -Path 'C:\Temp\out.txt' -Value 'another line'

02What it does

These cmdlets mirror '>' and '>>' but are explicit and scriptable, and both accept pipeline input: Get-Process | Set-Content procs.txt. Add-Content creates a missing file rather than erroring, which makes it ideal for building a report line by line. -NoNewline suppresses the trailing newline, and -Encoding controls the output encoding (default differs across PowerShell versions).

03Risk

RiskLow risk
RequirementsPowerShell
!Warning: Set-Content truncates the existing file with no prompt — don't point it at a file you need to preserve.

04Undo

Undo
$ Remove-Item 'C:\Temp\out.txt'

05Sources

  • Add-Content - PowerShell
    SS64 · technical · 2026-09-17
    Evidence: SS64 describes Add-Content appending to a file and creating it if it doesn't exist, with -Encoding and -NoNewline options.
  • Set-Content | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents Set-Content writing (overwriting) content to an item and Add-Content as the append counterpart.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions