Quick Tip. Getting an specific line in Get-Content


Hi guys, this is a quick tip and useful.

Lets say that you have a file with 10 lines and you want to  get only the last line :

(Get-Content c:\Test\lines.txt )[-1] 

Or if you want the last 3 lines :

(Get-Content c:\Test\lines.txt )[-1..-3]

If you want the first 2 lines :

(Get-Content c:\Test\lines.txt )[0..1]

Or you can use the totalcount parameter :

Get-Content c:\Test\lines.txt -totalcount 2

Or a specific line, lets say line 4 :

(Get-Content c:\Test\lines.txt )[3]

These examples ,excluding the totalcount parameter, are using an array to address the line, and in PowerShell the array is zero based, so to get the line 4 needs to refer to 3 ,or to get the first 2 lines needs to use 0 to 1 Alegre

#PowerShellLifeStyle

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s