Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need an equivalent of the Unix head command (display the first N lines of the output). This is what I'm using currently:

tasklist | find /N " " | findstr /r \[[0-9]\]

The above code displays the first 10 lines of tasklist's output. find /N " " prepends a line number to the start of each line while findstr /r \[[0-9]\] extracts the first 10 lines using regex.

Above code works, but I need to specify any range. Due to the fact that regular expressions are not implemented according to the standards in Windows, I can't get anything else to work.

How can I extract arbitrary lines from a cmd output? It is important to do this with a one-liner. No scripts!

share|improve this question
..................... – giecsar Mar 28 '13 at 15:55


PS C:\> netstat | Select -First 20

Edit: I have a feeling that you're going to insist that you're only able to use cmd.exe circa 1989, but that's not true. Powershell is baked into every OS version Vista+, and is installable on XP/2003. It is the future of Windows.

Edit: Alright, have it your way.

C:\> netstat -an > temp.txt && for /l %l in (1,1,10) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" temp.txt ^| findstr /r "^%l:"') do @echo %b

Will display the first 10 lines of the output of netstat.

share|improve this answer
I have no doubt that PowerShell is the future, but I need something that works, now. In an enterprise reality, often the customers of our tech support department have hundreds of windows servers, none of which have Vista, 7 or 8. I can't possibly waste time installing PowerShell every time I need to run a command, nor am I even authorized to. – giecsar Mar 28 '13 at 15:56
Cmd.exe it is... – Ryan Ries Mar 28 '13 at 16:35
all they would need is one w2k8r2 server that they CAN log into to run a powershell command, is that possible? – tony roth Mar 28 '13 at 16:46
I don't know if there are 2K8 servers, but Ryan Ries's answer is exactly what I needed. Thank you very much Ryan! – giecsar Mar 28 '13 at 17:02
Please don't forget to... mark the answer as... accepted... :`( curls up in fetal position in the corner and sobs to self – Ryan Ries Apr 1 '13 at 23:18

If you are a Unix type person, perhaps installing CygWin gives you a more familiar environment.

share|improve this answer
No, I thought I made it clear, but I can't installing anything. It must run with the Windows command processor (cmd.exe). No PowerShell, no CygWin, no scripts. – giecsar Mar 28 '13 at 16:00

The "loop way" suggested by the other answer works fine. However piping through findstr works too and may be simpler to type.

Aside from the unsupported features, findstr's regex is actually working properly. Your issue is with escaping. (See Rob's reference for details.) E.g. if you want to output lines 26 to 74, use:

tasklist|findstr/n ^^|findstr "^2[6-9]: ^[3-6][0-9]: ^7[1-4]:"


  1. First, dump the whole output using tasklist.

  2. Next, prepend each line with "$Num:" using findstr/n ^^

    • ^ means match start of line, and ^^ is its escaped version.

    • Note: you need to use findstr for this because find/n" " will not match lines without spaces`.

  3. Lastly, crop the output using findstr "^2[6-9]: ^[3-6][0-9]: ^7[1-4]:".

    • ^2[6-9]: is regex for 26 to 29.

    • ^[3-6][0-9]: is regex for 30 - 69.

    • ^7[1-4]: is regex for 71 to 74.

    • findstr "a b c" means find strings which match either a, b, or c.

Sample output:

26:RtkAudioService64.exe         1388 Services                   0      5,680 K
27:RAVBg64.exe                   1404 Console                    1      8,828 K
28:RAVBg64.exe                   1412 Console                    1      8,520 K
29:wlanext.exe                   1600 Services                   0      5,560 K
30:conhost.exe                   1612 Services                   0      2,656 K
31:spoolsv.exe                   1712 Services                   0     20,332 K
32:svchost.exe                   1740 Services                   0     39,320 K
share|improve this answer
That's a nice solution, but you now have the number prefixed there, can one get rid of them in the last findstr call? – fschmitt Sep 30 '15 at 15:26
@fschmitt, findstr cannot remove things. I'd think you might be forced to use a loop then in that case. – Pacerier Nov 26 '15 at 4:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.