Wednesday, February 18, 2015

#45 : Display top n lines or last n lines of a file

Often we reach to a situation where we need to get top 10 or 100 lines of a file.
These commands are fairly simple in Unix shell programming and most of you must have used Head and Tail commands. Those commands work really fast and accurate.

Keeping these points in mind, I dig something and want to share will all of you.

How to use Head and Tail Commands in Powershell?

To get the top 10 lines -
Get-Content ".\file_test.txt" | select -First 10

To get the top 10 lines -

Get-Content ".\file_test.txt" | select -Last 10

Easy! This is power of Powershell !

Some more Examples:

Problem: Write a command to get 3rd line of a file.

Get-Content ".\file_test.txt" | select -First 3 | select -Last 1

Problem: Write a command to skip 10 lines from top and display rest all lines of file.

Get-Content ".\file_test.txt" | select -Skip 10

But this approach has one drawback, when file is so big, it takes a little more time to produce results.

To be able to read a big file, you can adjust Get-Content to read only few lines. As you saw, we were reading the complete file before that caused more memory consumption.

Enjoy Scripting!!


  1. This comment has been removed by a blog administrator.

  2. Even better. But still last 5 lines might take longer time for large files.

  3. to get last lines of large files use:
    get-content $myFile -tail 5

  4. - tail rockz thx !

  5. Top 5: get-content $targetFile -totalcount 5

    Don't pipe if you don't have to!

  6. These are the examples for extracting the data from a file, but i want extract data from command output, so how can i achieve this?

    1. Simply put your data into a variable, then pipe that into the select command.

      For example:
      $data = get-process
      Then $data | select -last 10

  7. Is there any way to search between two occurrences of a word.

  8. I am trying to pull the last few lines from huge files and this seems to take forever... I'm still waiting (minutes) for the first attempt to work. I would love the -tail option but it does not seem to work in my powershell. At least the pipe to select works:

    PS C:\temp> Get-Content .\remapp_printers.txt | select -last 3
    \\mhmv-qs-100v\277R100HPCLJ4525DN - 1a
    \mhmv-qs-10 = plk-fs-01 - 1b
    New default printer = \\mhmv-qs-100v\15 PCL6 - 12/5/2016 - 6
    PS C:\temp> Get-Content .\remapp_printers.txt -tail 3
    Get-Content : A parameter cannot be found that matches parameter name 'tail'.
    At line:1 char:40
    + Get-Content .\remapp_printers.txt -tail <<<< 3
    + CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBin
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm

    1. This comment has been removed by the author.

  9. Hi Kevin and All,

    I was searching for some Live online help for Powershell.
    However i didn't find any suitable online live group

    So, i thought to create a Skype Group.
    Here we can help each other to solve any Powershell issue or puzzle.

    Please join me on

    Let's help each other. we really appreciate your help


  10. Thanks for sharing such important information which is very useful .I really appreciate your way of work.
    trend micro geek squad
    geek squad chat
    geek squad support
    webroot geek squad

  11. Thank you for this information of displaying top n lines or last n lines of a file. If you want to get the top 10s news all over the world then, visit The Youth.

  12. Interesting Article. Hoping that you will continue posting an article having a useful information. Fundamentos de PowerShell

  13. It is really a helpful blog to find some different source to add my knowledge. I came into aware of new professional blog and I am impressed with suggestions of author. click read

  14. Automotive onboard panel display due to high technical specifications, good profit, has always been the LCD panel display suppliers-related manufacturers of a competitive place. In the first half of 2020, the global auto industry was in a depressed market due to coVID-19, lockdowns, and factory shutdowns. However, as economic activity gradually restarts and auto factories resume production, the industry is very positive about the recovery of the automotive panel display market in 2021. Among them, the trend of electric vehicle growth is more concerned.

  15. Can I use this in my smart phone device.

  16. Excellent post, I wonder if you can
    Write more on the subject? If you can
    Further details would be appreciated. Appreciate it!


#100: How to execute a string in Powershell?

 Rarely, but sometimes we need to run the command from an expression that we create. I stumbled upon a situation where I was to read the con...