Set-Content
Overwrite a text file
Set-Content -Path .\notes.txt -Value 'Hello'
- Works on
- WindowsLinuxmacOS
- PowerShell
- 5.1 and later
- Reference
- Microsoft Learn: Set-Content
Careful: Set-Content replaces the whole file.
- PowerShell 5.1 Writes ANSI by default. Add -Encoding utf8 for accented text.
Writes new content and wipes the old. Use Add-Content to append. On Windows PowerShell 5.1 the default encoding is often not UTF-8. PowerShell 7 writes UTF-8.
Try this
Set-Content -Path .\notes.txt -Value 'Hello' -WhatIf
Coming from another shell
| Shell | What you might type |
|---|---|
| bash / zsh | echo 'Hello' > notes.txt |
| cmd.exe | echo Hello> notes.txt |
More in From bash and From cmd.