Set-Content
Use Set-Content to replace the entire contents of a file with the value you supply.
Documentation: Microsoft Learn — Set-Content
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Set-Content [-Path] <String[]> [-Value] <Object[]> [-Encoding <Encoding>] [-Force] [-NoNewline] [-WhatIf] [<CommonParameters>]- Stage 1Set-Content
-Path -Value -Encoding -Force -NoNewline -WhatIf
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Set-Content -Path 'C:\Temp\note.txt'
Show reference output
Sample output · fictional data, shaped like the real thing
first line
Set-Content -Path 'C:\Temp\list.txt'
Show reference output
Sample output · fictional data, shaped like the real thing
one two three
Change with Set-Content
Show reference output
Sample output · fictional data, shaped like the real thing
# Set-Content overwrites; Add-Content appends
Parameters
Parameter definitions:
-Path-Value-Encoding-Force-NoNewline-WhatIf
Practical use and troubleshooting
Practical scenario: Use Set-Content when you need to replace the entire contents of a file with the value you supply. Review syntax and sample output, then try an example in the browser lab. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: Unexpected loss of earlier text is often the result of replacement rather than append semantics. Restore the file from a backup and use Add-Content for incremental logging. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Add-Content. Add-Content appends to existing content; Set-Content replaces it. Choose deliberately when maintaining a log rather than creating a fresh report. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Set-Content Use Set-Content to replace the entire contents of a file with the value you supply.
Next: Add-Content