Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell Get Content

Posted on February 13, 2007July 2, 2013

One slick trick you can do in PowerShell with text files that's difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you want displayed like this:

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

(get-content c:\boot.ini)[0]

Don't forget that arrays start counting at 0. If you want to see a range of lines, simply specify the range:

(get-content c:\boot.ini)[0..5]

This will display the first 6 lines of c:\boot.ini. Want to look at the last X number of lines? Well you first need to get the number of lines.

(get-content c:\boot.ini).Length

Once you know the length, specify whatever range you want. My boot.ini is 7 lines long so if I wanted to see the last 3 lines I would simply run

(get-content c:\boot.ini)[5..7]

Of course, this little tidbit wouldn't be complete without a one-liner to get the last 5 lines without knowing in advance how long the file is:

$c=get-content c:\boot.ini;$l=$c.length;$c[($l-5)..$l]

If you specify a number greater than the value of $l then the entire file will be returned.

Obviously the boot.ini file is hardly an exciting thing to test so try this out on log files on your systems.

These commands are very similar to the Head and Tail utilities and in fact you could easily turn these into your own Head and Tail functions.


Behind the PowerShell Pipeline
Technorati tags: PowerShell, Scripting, GetContent, cmdlet

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

8 thoughts on “PowerShell Get Content”

  1. Vinicius Canto says:
    February 14, 2007 at 7:05 pm

    Hi,

    I have posted another solution (and some comments) about the last command, to show only the last 5 lines of a file…

    Instead of

    $c=get-content c:\boot.ini;$l=$c.length;$c[($l-5)..$l]

    you can use

    (cat c:\boot.ini)[-5..-1]

    The only problem is… my blog is in Portuguese. There are few content about Powershell and scripting in my country…

    If you have some time:
    http://viniciuscanto.blogspot.com/2007/02/tail-x-get-content-obtendo-as-ltimas.html

    Tks!

    —
    Vinicius Canto < scripterbr_at_gmail_dot_com >
    MVP Visual Developer – Scripting
    MCP Windows 2000 Server, Windows XP e SQL Server 2000
    Blog sobre Scripting: http://viniciuscanto.blogspot.com

  2. Jeffery Hicks says:
    February 15, 2007 at 7:36 am

    Using the alias, cat, certainly makes it easier. Just as a point of reference, I try to avoid using aliases so that people understand what cmdlet is really doing the work.

    Your comment certainly works if you know the length of the file. I also should have pointed out that you can use:

    cat c:\boot.ini -totalcount 3

    and that will return the first 3 lines. -1 means return everything so you can use that to get the end of the file. But certainly a handy HEAD subsitute.

  3. Vinicius Canto says:
    February 15, 2007 at 8:39 pm

    Yeah, great…

    But my recommendation works even when you don’t know de lenght of the file. If you use an invalid interval (like use [-4000..-1] with a file with only four lines, the command still works.

    By curiosity, I found on the Internet the legend, the original Tail command, but for Windows!
    http://tailforwin32.sourceforge.net/

    I haven’t tested yet, but I liked.

    Congratulations for you blog… it’s very good. It’s in my blog list now!

    Vinicius

  4. Jeffery Hicks says:
    February 15, 2007 at 9:28 pm

    I must have had fumbled fingers the first time I tried your suggestion because it didn’t work for me. Maybe I missed the minus signs. But I see now what you are talking about.

    Thanks.

  5. Anonymous says:
    March 2, 2009 at 5:31 pm

    Unlike tail, which I believe reads the file in reverse n number of lines, using [-n..-m] or | select -last n seems to require PS to read the entire file and then just return what you’ve selected. Try using either of the proposed PS techniques to “tail” a 300 MB file. It takes five minutes, compared to 5 seconds with tail.

    For a tail equivalent, PS would need to filter the input, not the output.

  6. Anonymous says:
    July 22, 2009 at 10:55 pm

    I'm just trying to find Linux equivalents for PS to work on our big log files on Windows and its true, the PS commands are taking about 3 minutes to get a result back which is not acceptable for me. Hope that there is another solution to read the file from the end instead of reading the whole file.

    Any ideas yet?
    Thanks.

  7. Jeffery Hicks says:
    July 23, 2009 at 10:15 am

    You might wait and try using the version of Get-Content in PowerShell v2.0. Performance with large text files is always an issue.

  8. Jeffery Hicks says:
    July 23, 2009 at 10:19 am

    I just ran

    PS C:\windows> (gc .\WindowsUpdate.log)[-10..-1]

    In PowerShell v2 on a log file with 16511 lines and it took hardly any time at all to grab the last 10 lines.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d