out-file — Write pipeline objects to a file — bash > redirection equivalent across all 5 shells
Equivalents in every shell
command > out.txtShell-level redirection. `>` truncates; `>>` appends. `2>` redirects stderr; `2>&1` merges stderr into stdout. `command > out.txt 2>&1` is the canonical "capture everything" form. `tee` duplicates to both file and stdout.
command > out.txtSame `>` / `>>` / `2>` / `&>` semantics as bash. Zsh adds `MULTIOS` (on by default): `echo hi >file1 >file2` writes to BOTH files at once — surprising if you expected the last redirection to win.
command > out.txtSame `>` / `>>` forms. Fish writes `>?` for "redirect to file only if it does not exist" (noclobber semantics in one symbol) and `&>` for combined stdout+stderr. No multi-out (no zsh-style `>file1 >file2`).
command | Out-File out.txt`Out-File` writes the FORMATTED string representation of objects (the same output `Format-Table` would emit). Default encoding is UTF-16 LE on Windows PowerShell 5.1 and UTF-8 (no BOM) on pwsh 7+. `-Encoding utf8` forces UTF-8; `-Append` to append; `-NoNewline` to omit trailing newline. The shorthand `>` operator is sugar for `Out-File`.
command > out.txtSame `>` / `>>` semantics as Unix. `2>` redirects stderr; `2>&1` merges. cmd has no equivalent to Unix `tee` — install GnuWin32 `tee.exe` or shell out to `powershell -Command "... | Tee-Object out.txt"`.
Worked examples
Save command output to a file (overwrite)
ls -la > listing.txtGet-ChildItem | Out-File listing.txtdir > listing.txtAppend output to an existing file
echo "$(date)" >> log.txtGet-Date | Out-File log.txt -Appenddate /t >> log.txtWrite UTF-8 without BOM (portable)
command > out.txtcommand | Out-File out.txt -Encoding utf8NoBOMGotchas
- `Out-File` emits the FORMATTED string representation — what you'd see in the console — including column headers, padding, and ellipses on wide objects. For raw object DATA, use `Export-Csv`, `ConvertTo-Json | Set-Content`, or `Set-Content` (which writes the `.ToString()` of each object, one per line, no formatting).
- Encoding defaults bite: Windows PowerShell 5.1 writes UTF-16 LE with a BOM (`-Encoding Unicode`) — invisible files for Unix tools. Pwsh 7+ defaults to UTF-8 NO BOM. ALWAYS pass `-Encoding utf8NoBOM` (pwsh 7) or `-Encoding ([System.Text.UTF8Encoding]::new($false))` (5.1 hack) for portable scripts.
- Wide-output truncation: `Get-Process | Out-File procs.txt` may CLIP property values at the console window width, because the cmdlet asks the formatter to render at the host's current width. To avoid truncation, pass `-Width 9999` or pipe through `Format-Table -AutoSize` first.
- The `>` operator IS `Out-File` — `Get-Process > procs.txt` is identical to `Get-Process | Out-File procs.txt`. Both share the same formatting + encoding gotchas. For verbatim string output (no formatting), use `Set-Content`: `Get-Content in.txt | Set-Content out.txt` round-trips exactly, `Out-File` does not.
- `-NoClobber` errors instead of overwriting if the file exists — the safe-default equivalent of bash `set -o noclobber`. Without it, `Out-File` happily truncates existing files. For scripts that should never overwrite, set the preference variable: `$PSDefaultParameterValues['Out-File:NoClobber'] = $true`.