Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to display the first N lines of the output of a command (any command). In other words, I esentially want an equivalent of the Unix "head" command. After discovering a few tricks from some other user (sorry don't remember who it was), I came up with this:

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

In the example above, the command processor display the first 10 lines of the tasklist command output.

The command:

find /N " "

adds the line number at the beginning of each line.

The command:

findstr /r \[[0-9]\]

extracts the first 10 lines using a regular expression.

Now, I would like to be able to specify any range, but 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 the first 20 lines, for example? It is important to do this with a one-liner. No scripts!


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

2 Answers 2


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

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.