DEV Community

Cover image for PowerShell Set-Content: Write Text to Files Fast
arnostorg
arnostorg

Posted on

PowerShell Set-Content: Write Text to Files Fast

The PowerShell Set-Content cmdlet writes text into a file. It is the clear, readable way to create a new file or replace what is already there — cleaner than juggling > redirects when you want intentional writes in scripts.

For the official reference, see Microsoft Learn’s Set-Content.

Want to practice PowerShell in a live browser terminal (no install)? Try the free interactive lessons on CMD Master — PowerShell file and text commands.

Write a simple file

Set-Content -Path .\hello.txt -Value "Hello from PowerShell"
Enter fullscreen mode Exit fullscreen mode

That creates hello.txt (or fully replaces it if it already exists) with one line of text. Multiple strings become multiple lines:

Set-Content -Path .\notes.txt -Value "Line one", "Line two", "Line three"
Enter fullscreen mode Exit fullscreen mode

Replace vs append

Set-Content replaces the whole file. If you need to add lines without wiping what is there, use Add-Content instead:

# wipe and rewrite
Set-Content -Path .\log.txt -Value "Fresh start"

# keep existing lines, add more
Add-Content -Path .\log.txt -Value "Another entry"
Enter fullscreen mode Exit fullscreen mode

Remembering that split saves a lot of accidental data loss: Set-Content means “make the file equal this”; Add-Content means “append.”

Write output from other commands

Pipe any string output into Set-Content:

Get-ChildItem -Name *.ps1 | Set-Content -Path .\scripts-list.txt
Get-Date -Format "yyyy-MM-dd HH:mm" | Set-Content -Path .\last-run.txt
Enter fullscreen mode Exit fullscreen mode

That is handy for snapshots, inventories, and small status files your scripts leave behind.

Encoding matters

On Windows, default encoding can surprise you when tools expect UTF-8. Be explicit when you care:

Set-Content -Path .\config.json -Value '{"env":"dev"}' -Encoding utf8
Enter fullscreen mode Exit fullscreen mode

Use -Encoding utf8 (or utf8BOM / unicode when a consumer needs those) so cross-tool files stay readable.

Create folders first when needed

Set-Content writes the file. If the parent folder is missing, create it first:

New-Item -ItemType Directory -Path .\out -Force | Out-Null
Set-Content -Path .\out\readme.txt -Value "Build artifacts go here"
Enter fullscreen mode Exit fullscreen mode

Safe overwrite pattern

When a file already holds something important, back it up, then rewrite:

if (Test-Path .\app.config) {
  Copy-Item .\app.config .\app.config.bak -Force
}
Set-Content -Path .\app.config -Value $newConfig -Encoding utf8
Enter fullscreen mode Exit fullscreen mode

That habit pairs well with Copy-Item and keeps experiments reversible.

Quick checklist

Goal Cmdlet
Replace / create file text Set-Content
Append lines Add-Content
Read file text Get-Content
Copy the file itself Copy-Item

Practice it live

CMD Master is a free online interactive learning platform for the command line: practice in a live browser terminal with instant feedback, no install or VM. It covers Windows CMD, PowerShell, and Bash.

Start here: CMD Master — interactive PowerShell practice.

Top comments (0)