set-content — Write a string to a file (overwrite) — bash > redirection across all 5 shells
Equivalents in every shell
echo "hello" > file.txtShell-level redirection. `>` truncates and writes; `>>` appends. Bytes flow as-is — no formatting, no encoding conversion (the file is whatever the writing process emits).
echo "hello" > file.txtSame `>` redirection as bash, plus `MULTIOS` (writes to multiple targets if you redirect twice on one line) and `>!` to override `noclobber`.
echo "hello" > file.txtSame `>` redirection. Fish adds `>?` — noclobber semantics in one symbol (errors if the file exists). Bytes are written as-is.
Set-Content -Path file.txt -Value "hello"Writes the `.ToString()` of each input object, one per line, with NO formatting (unlike `Out-File`). `-NoNewline` to omit the trailing line break; `-Encoding utf8` to force UTF-8 (default is UTF-16 LE w/ BOM on Win PS 5.1; UTF-8 NoBOM on pwsh 7+); `-AsByteStream` (pwsh 6+) for raw binary writes.
echo hello > file.txtSame `>` semantics as Unix. `>>` to append. cmd `echo` does NOT strip surrounding double-quotes — `echo "hello" > x` writes `"hello"` WITH the quotes; omit the quotes for unquoted text.
Worked examples
Write a string to a file (overwrite)
echo "hello" > file.txtSet-Content -Path file.txt -Value "hello"echo hello > file.txtRound-trip a file through a transformation without re-formatting
sed 's/foo/bar/g' in.txt > out.txt(Get-Content in.txt) -replace 'foo','bar' | Set-Content out.txtWrite raw bytes (binary safe)
printf "\x00\xff" > raw.binSet-Content -Path raw.bin -Value ([byte[]](0,255)) -AsByteStreamGotchas
- `Set-Content` writes the `.ToString()` of each input — NOT the formatted/console representation. `Get-Process | Set-Content procs.txt` writes lines like `System.Diagnostics.Process (firefox)`, NOT the table you see in the console. For console-style formatting use `Out-File`; for structured data use `Export-Csv` or `ConvertTo-Json | Set-Content`.
- Encoding default differs by version: Windows PowerShell 5.1 writes UTF-16 LE WITH BOM; pwsh 6+ writes UTF-8 NO BOM. Scripts run on both versions produce non-byte-identical files unless you pass `-Encoding utf8NoBOM` (pwsh 7) explicitly. This breaks tooling that hashes files or diffs against canonical text.
- `Set-Content` adds a trailing newline after the last line by default. Pass `-NoNewline` to suppress it — required when round-tripping files whose terminal-newline state matters (config files, certain git blobs, header-only HTTP captures).
- Pipeline input is BATCHED into one file write per item: `1..10000 | Set-Content out.txt` opens, writes, and re-opens the file repeatedly — pathologically slow on large arrays. For bulk writes, materialise first: `(1..10000) -join "`n" | Set-Content out.txt`, or use `[IO.File]::WriteAllLines()` for true bulk performance.
- On Windows, `Set-Content` honours the `ReadOnly` file attribute — it ERRORS if the target is read-only. Pass `-Force` to overwrite (which clears the attribute). Unix permissions (`chmod -w`) are honoured equivalently on pwsh 6+ Linux/macOS.