PowerShell Genie

Set-Content

Use Set-Content to replace the entire contents of a file with the value you supply.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Set-Content

Syntax

1PS> Set-Content [-Path] <String[]> [-Value] <Object[]> [-Encoding <Encoding>] [-Force] [-NoNewline] [-WhatIf] [<CommonParameters>]
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>]
  1. Stage 1Set-Content-Path -Value -Encoding -Force -NoNewline -WhatIf

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Set-Content -Path 'C:\Temp\note.txt'

    1PS> Set-Content -Path 'C:\Temp\note.txt' -Value 'first line' -Encoding utf8
    Show reference output

    Sample output · fictional data, shaped like the real thing

    first line

    Set-Content -Path 'C:\Temp\list.txt'

    1PS> Set-Content -Path 'C:\Temp\list.txt' -Value @('one' , 'two' , 'three') -Encoding utf8
    Show reference output

    Sample output · fictional data, shaped like the real thing

    one
    two
    three

    Change with Set-Content

    1PS> # Set-Content overwrites; Add-Content appends
    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

    Set-Content on Microsoft Learn

    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.

    Set-Content reference on Microsoft Learn

    Recap

    Set-Content Use Set-Content to replace the entire contents of a file with the value you supply.

    Next: Add-Content
    Genie guideSet-Content

    Use Set-Content to replace the entire contents of a file with the value you supply. The console below is pre-loaded — run it, then change a value and run it again.