Skip to content
shellmap

out-fileWrite pipeline objects to a file — bash > redirection equivalent across all 5 shells

Equivalents in every shell

Bashunix
command > out.txt

Shell-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.

Zshunix
command > out.txt

Same `>` / `>>` / `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.

Fishunix
command > out.txt

Same `>` / `>>` 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`).

PowerShellwindows
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`.

cmd.exewindows
command > out.txt

Same `>` / `>>` 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)

Bash
ls -la > listing.txt
PowerShell
Get-ChildItem | Out-File listing.txt
cmd.exe
dir > listing.txt

Append output to an existing file

Bash
echo "$(date)" >> log.txt
PowerShell
Get-Date | Out-File log.txt -Append
cmd.exe
date /t >> log.txt

Write UTF-8 without BOM (portable)

Bash
command > out.txt
PowerShell
command | Out-File out.txt -Encoding utf8NoBOM

Gotchas

  • `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`.

WSL & PowerShell Core notes

pwsh`Out-File` is fully cross-platform. The `>` operator alias for `Out-File` works everywhere too. The biggest cross-platform footgun is the encoding default difference between Windows PowerShell 5.1 (UTF-16 LE w/ BOM) and pwsh 6+ (UTF-8 no BOM) — scripts that work on Linux/macOS pwsh may produce unreadable files on Windows pwsh 5.1 unless `-Encoding utf8NoBOM` is set explicitly. On pwsh 7+ this is the default everywhere.
WSLFrom inside WSL bash, `command > out.txt` writes a Linux file (LF line endings, UTF-8). If you save to `/mnt/c/...` the file lives on NTFS but the bytes are LF-line-ending UTF-8 — Windows Notepad and modern editors handle this fine; old text editors may show all content on one line. For files that must be Windows-readable with CRLF, pipe through `unix2dos` or `sed -e 's/$/\r/'` before redirecting.

Related commands