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

More Fun with Get-Content

Posted on February 15, 2007July 2, 2013

A few followups on my recent post about Get-Content. First, you can also use the CAT alias if you're used to the Unix world, or TYPE in place of get-content. The Get-Content cmdlet does have a parameter called -totalcount that will return the specified number of lines from the beginning of the file:

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!

cat c:\boot.ini -totalcount 3

Unfortunately, you can't specify a range or start at the end of the file. However, there is a way to simplify the one-liner I used. You can use the built in common parameters and specify a variable with Get-Content. This command will get the last five lines of the file log.txt:

cat log.txt -outvariable c >$null ; $c[($c.count-5)..($c.count)]

I'm redirecting the output of the first half of the expression to $null otherwise I'd see the entire file in addition to the last five lines.

That is much more efficient. Now I don't have to get the count or length ahead of time. And if you wanted to wrap this up in a function, it is pretty simple:

Function GetTail {
param([string]$file,[int]$lines)
cat $file -outvariable c >$null ; $c[($c.count-$lines)..($c.count)]

}

To use:

GetTail log.txt 5

Will return the last five lines of log.txt.

Technorati tags: PowerShell, Scripting, GetContent, Cmdlet, Functions


Behind the PowerShell Pipeline

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

3 thoughts on “More Fun with Get-Content”

  1. Jeffery Hicks says:
    February 15, 2007 at 9:32 pm

    Here’s an even more efficient function:
    Function GetTail {
    param([string]$file,[int]$lines)
    (cat $file)[-$lines..-1]
    }

    Thanks to Vinicius Canto for the tip.

  2. Dus says:
    May 8, 2008 at 11:50 am

    I have an excel document that has 7 columns and I want to get the last column of data. How do you specify which column you want get-content to pull from?

  3. Jeffery Hicks says:
    May 12, 2008 at 9:18 am

    dus,
    You can only use Get-Content with text files. To work with Excel you would need to use the Excel COM object just as you would with VBScript, although with PowerShell it can be interactive. If you can save your data as a CSV you could also use the Import-CSV cmdlet. Each line would be an object. Its properties would be the column headings. You would use something like:

    import-csv mydata.csv | Select myproperty

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