How to Use Set-Content in PowerShell

4 min read James Futhey Updated November 18, 2026
Microsoft Learn documentation for the Set-Content cmdlet showing its PowerShell syntax

Quick answer: Set-Content writes text to a file. It creates the file if it doesn't exist and replaces everything in it if it does. To add to the end of a file instead, use Add-Content.

Set-Content -Path .\notes.txt -Value 'Hello, World'

To read the file back, use Get-Content.

Syntax

Set-Content
    [-Path] <string[]>
    [-Value] <Object[]>
    [-PassThru]
    [-Filter <string>]
    [-Include <string[]>]
    [-Exclude <string[]>]
    [-Force]
    [-Credential <pscredential>]
    [-WhatIf]
    [-Confirm]
    [-NoNewline]
    [-Encoding <Encoding>]
    [-AsByteStream]
    [-Stream <string>]
    [<CommonParameters>]

A second parameter set takes -LiteralPath <string[]> in place of -Path. -NoNewline, -Encoding, -AsByteStream and -Stream come from the FileSystem provider, so they only work on file system drives.

Parameters

Parameter What it does
-Path The file to write. Required, position 0. Wildcards allowed, so .\Test*.txt writes to every match.
-LiteralPath Like -Path, but no character is read as a wildcard. Use it for names with [ or ]. Alias PSPath.
-Value The new content. Required, position 1. Also accepts pipeline input. An array writes one item per line.
-Encoding The file's encoding. See the table below.
-NoNewline Joins the values with no spaces or newlines, and adds no newline at the end.
-AsByteStream Writes bytes instead of text. PowerShell 6.0 and later. Ignores -Encoding and warns if you pass both.
-Stream Writes to an NTFS alternate data stream, creating it if needed. Windows only.
-Force Writes to a read-only file. It doesn't override security restrictions.
-Filter, -Include, -Exclude Narrow which files a wildcard path matches. They only take effect when the path names a folder's contents, such as C:\Temp\*.
-PassThru Returns the content written. By default the cmdlet outputs nothing.
-WhatIf, -Confirm Preview the change, or ask before making it.
-Credential Not supported by any provider that ships with PowerShell. Use Invoke-Command to run as another user.

Default encoding: PowerShell 7 vs 5.1

Version Default -Encoding Other values
PowerShell 7 utf8NoBOM (UTF-8 without a byte order mark) ascii, ansi (7.4+), bigendianunicode, bigendianutf32, oem, unicode, utf7, utf8, utf8BOM, utf32, or a code page such as 1251 (6.2+)
Windows PowerShell 5.1 Default: the system's active code page, usually ANSI Ascii, BigEndianUnicode, BigEndianUTF32, Byte, Oem, String, Unicode (UTF-16 little-endian), UTF7, UTF8, UTF32

Two differences catch people out:

  • Files differ between versions. The same command writes ANSI in 5.1 and UTF-8 in 7. Pass -Encoding when another program reads the file.
  • Bytes. 5.1 has no -AsByteStream. It uses -Encoding Byte instead.

Microsoft no longer recommends UTF-7. PowerShell 7.1 and later warn if you use it.

Examples

Create a file or replace its text

Microsoft's example writes the current date and time to a new file:

Set-Content -Path .\DateTime.txt -Value (Get-Date)
Get-Content -Path .\DateTime.txt

The date object is written as a string.

Write several lines

Set-Content -Path .\servers.txt -Value 'server01', 'server02', 'server03'

Write a block of text with a here-string

@'
[settings]
mode=production
retries=3
'@ | Set-Content -Path .\app.ini

Find and replace text in a file

(Get-Content -Path .\Notice.txt) |
    ForEach-Object {$_ -replace 'Warning', 'Caution'} |
        Set-Content -Path .\Notice.txt

The parentheses make Get-Content finish reading before Set-Content writes to the same file.

Replace the text in many files at once

Set-Content -Path .\Test*.txt -Value 'Hello, World'

To match by filter, end the path with *:

Set-Content -Path C:\Temp\* -Filter *.txt -Value 'Empty'

Choose an encoding

Set-Content -Path .\export.csv -Value 'Name,Email' -Encoding utf8BOM

utf8BOM is PowerShell 7 only. Windows PowerShell 5.1 doesn't accept it.

Write without a trailing newline

Set-Content -Path .\token.txt -Value 'abc123' -NoNewline

Copy a binary file (PowerShell 7)

Get-Content -Path .\logo.png -AsByteStream | Set-Content -Path .\logo-copy.png -AsByteStream

Write to an alternate data stream (Windows)

Set-Content -Path .\report.txt -Stream 'notes' -Value 'Reviewed'
Get-Content -Path .\report.txt -Stream 'notes'

Overwrite a read-only file

Set-Content -Path .\readonly.txt -Value 'New content' -Force

Set-Content vs Add-Content vs Out-File

Cmdlet What it does
Set-Content Replaces the file's content with strings.
Add-Content Appends to the end of the file.
Out-File Writes objects to a file.

Set-Content is built for strings and converts anything else to a string before writing it. To write objects to files, Microsoft points to Out-File.

Troubleshooting

Find and replace on the same file goes wrong. Wrap the Get-Content command in ( ) so it finishes reading before Set-Content writes, as in Microsoft's example.

Strange characters in another program. The encoding doesn't match what that program expects. 5.1 writes ANSI by default and 7 writes UTF-8 without a BOM. Set -Encoding explicitly.

Access denied on a read-only file. Add -Force. If it still fails, the error is a permission problem, which -Force doesn't bypass.

A file name with brackets isn't found. -Path reads [ and ] as wildcards. Use -LiteralPath.

-Include or -Exclude does nothing. The path must point at the folder's contents, for example C:\Temp\*.

-AsByteStream isn't recognized. You're on Windows PowerShell 5.1. Use -Encoding Byte there.

-Stream isn't recognized. Alternate data streams only exist on Windows.

FAQ

Does Set-Content overwrite the file?

Yes. It replaces all existing content. Use Add-Content to append instead.

Does Set-Content create the file if it doesn't exist?

Yes. To create files or folders ahead of time, use New-Item.

What is the default encoding of Set-Content?

utf8NoBOM in PowerShell 7. Default, the system's active code page (usually ANSI), in Windows PowerShell 5.1.

What is the alias for Set-Content?

Windows PowerShell 5.1 has the alias sc. Microsoft's PowerShell 7 page doesn't list it, so write Set-Content in full in scripts that run on both.

How do I clear a file without deleting it?

Use Clear-Content.

How do I list the files before changing them?

Run Get-ChildItem with the same path, for example Get-ChildItem -Path .\Test*.txt.

Sources: Set-Content (PowerShell 7.5) and Set-Content (Windows PowerShell 5.1) on Microsoft Learn.

Managing Microsoft 365 rooms too? Meeting Room 365 turns any tablet into a room display for Outlook and Teams calendars.

Add a display to your office

Every room, on the door.

Live availability on every meeting room. Works with Microsoft 365, Exchange and Google Workspace.