Windows Equivalent of Unix Head Command: Best Methods to Extract First N Lines from Command Output


1 views

When working with Windows command line, many developers miss the simplicity of Unix's head command. While your current approach using find and findstr works for the first 10 lines, it lacks flexibility for arbitrary ranges. Let's explore better solutions.

For getting the first N lines, these methods work without external tools:

:: Method 1: Using more +n (for first 10 lines)
tasklist | more +10

:: Method 2: PowerShell one-liner (more flexible)
powershell -command "tasklist | select -first 15"

PowerShell provides the most elegant solution with its Select-Object cmdlet (alias: select):

:: First 20 lines of any command
your_command | powershell -command "$input | select -first 20"

:: Specific line range (lines 5-15)
your_command | powershell -command "$input | select -skip 4 -first 11"

For environments where PowerShell isn't available, this batch script technique works well:

@echo off
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%a in ('tasklist') do (
    set /a count+=1
    if !count! leq 12 echo %%a
)

Combine CMD and PowerShell for the best of both worlds:

:: First N lines (replace 8 with desired number)
cmd /v:on /c "set n=8 & for /f "delims=" %a in ('tasklist ^| find /v ""') do @(set /a n-=1>nul & if !n! geq 0 echo %a)"

For large outputs, PowerShell solutions generally perform better than pure CMD approaches. The Select-Object -First method stops processing once it reaches the required number of lines, while CMD alternatives typically process the entire input first.


When working with command-line outputs in Windows, many developers find themselves missing Unix utilities like head that can easily extract the first N lines of output. While Windows has powerful command-line tools, they don't always provide straightforward solutions for this common task.

Here are several ways to implement head functionality in Windows:

:: Method 1: Using more command
command | more +e /tN

:: Example: Get first 5 lines of ipconfig
ipconfig | more +e /t5
:: Method 2: Using findstr with line numbers
command | find /n /v "" | findstr /r /c:"^$$[1-N]$$"

:: Example: Get first 3 lines of systeminfo
systeminfo | find /n /v "" | findstr /r /c:"^$$[1-3]$$"

For more flexibility, PowerShell provides cleaner solutions:

# Get first N lines
command | Select-Object -First N

# Example: First 7 processes
Get-Process | Select-Object -First 7

If you need a reusable solution without PowerShell:

:: head.bat - Windows head equivalent
@echo off
setlocal enabledelayedexpansion
set counter=0
for /f "tokens=1* delims=:" %%a in ('%* ^| find /n /v ""') do (
    set /a counter+=1
    if !counter! leq %1 (echo.%%b)
)

Usage example: head.bat 5 tasklist will show first 5 lines of tasklist output.

The native Windows solutions process the entire input before filtering, which might be inefficient for large outputs. PowerShell's Select-Object -First is more efficient as it stops processing after reaching the specified number of lines.

Here are practical examples where this technique proves valuable:

:: Quick process monitoring
tasklist | more +e /t5

:: Checking network configuration
ipconfig | find /n /v "" | findstr /r /c:"^$$[1-8]$$"

:: Verifying system information
systeminfo | powershell -command "$input | Select-Object -First 10"

For production environments, consider creating an alias or adding the batch script to your PATH for easier access to this functionality.