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.
01Command
$ 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
04Undo
$ Remove-Item 'C:\Temp\out.txt'05Sources
- Add-Content - PowerShellEvidence: SS64 describes Add-Content appending to a file and creating it if it doesn't exist, with -Encoding and -NoNewline options.
- Set-Content | Microsoft LearnEvidence: 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
Diagnose NTFS file system errors and bad sectors on any volume — including the system drive while Windows is running — before committing to a repair.
Robocopy is Windows' built-in robust file copier: use /MIR to keep a destination folder as an exact one-way mirror of a source, resumable and restartable.
Search inside text files and logs with regex, returning file, line number and match.