Skip to content
shellmap

headOutput the first lines of a file across all 5 shells

Equivalents in every shell

Bashunix
head -n 10 file
Zshunix
head -n 10 file
Fishunix
head -n 10 file
PowerShellwindows
Get-Content file -TotalCount 10

Also `Select-Object -First 10` on streamed content.

cmd.exewindows
powershell -Command "Get-Content file -TotalCount 10"

Worked examples

Show first 5 lines

Bash
head -n 5 file.txt
PowerShell
Get-Content file.txt -TotalCount 5

Show first lines of multiple files

Bash
head -n 3 *.txt
PowerShell
Get-ChildItem *.txt | ForEach-Object { Get-Content $_ -TotalCount 3 }

Skip the first N bytes

Bash
head -c 1024 file
PowerShell
(Get-Content file -AsByteStream -TotalCount 1024)

Gotchas

  • PowerShell `-TotalCount` reads lines, while `-AsByteStream -TotalCount` reads bytes — be explicit about your unit.
  • GNU `head -n -5` removes the last 5 lines (negative count); BSD head (macOS) does not support negatives.
  • For very large files, `Get-Content` is line-by-line — use `[System.IO.File]::OpenRead` for byte-level streaming.

WSL & PowerShell Core notes

pwsh`Get-Content -TotalCount N` and the alternative `Get-Content | Select-Object -First N` differ in streaming: `-TotalCount` stops reading the file once N lines are seen (efficient on large files); `Select-Object -First N` triggers Get-Content to read the whole file before propagating Stop. Reach for `-TotalCount` for large-file scenarios.
WSL`head /mnt/c/path/to/file` works but goes through DrvFs — reading the first lines of a Windows-side file from WSL is ~5–10× slower than reading the same file from Windows directly. For interactive previewing of large Windows-side logs, `cmd.exe /c "more /e +0 file.log"` or Windows-side PowerShell `Get-Content` is faster.

Common tasks using head

Related commands