PowerShell- Get Specific lines of file

Very often Administrators has task to monitor log file like text file, csv file , windows log file, IIS log, error log, firewall log file to get first line of file or get top 10 lines of file.

Using PowerShell, administrators can easily get top lines of file using Get-Content cmdlet and First parameter with value to get specific lines from text file. Using PowerShell script, its very easy and quick to display top lines of text file or csv file.

First parameter specifies the number of the lines from the start at line of file.

Let’s understand with examples of using Get-Content cmdlet to get first 10 lines of file, file can be text file, csv file or log file.

Cool Tip: How to get certificates using PowerShell !

Using PowerShell to Get First Line of File

Using PowerShell, get first line of file using Get-Content First parameter.

PS C:\> Get-Content D:\MultipleLineExamples.txt | Select -First 1

1. This is First line
PS C:\>  

In the above example, to get first line of text file

  • Get-Content command reads the content of text file specified by path
  • Pass through content of file to pipe operator (|).
  • Pipe Operator pass it as input to read first line of the file using Select -First 1

PowerShell Get-Content First 10 lines of File

In PowerShell to select first 10 lines of file, use Get-Content command with Select -First parameter with value 10. Here -First 10 show top 10 lines of the file.

# Use First parameter to get top 10 lines of file
PS C:\> Get-Content D:\MultipleLineExamples.txt | Select -First 10                                                      

1. This is First line
2. This is Second line
3. This is Third line
4. This is Fourth line
5. This is Fifth line
6. This is Sixth line
7. This is Seventh line
8. This is Eighth line
9. This is Nineth line
10. This is Tenth line
PS C:\>    

In the above PowerShell script, Get-Content select first 10 lines of file using First parameter.

Cool Tip: Do you know equivalent of cat command in Windows!

PowerShell Get-Content Skip first line

In PowerShell, using Get-Content to skip first line of file, use Skip parameter with value to skip line. It will skip those number of lines and display rest of the lines of file.

PS C:\> Get-Content D:\MultipleLineExamples.txt | Select -Skip 1                                                        

2. This is Second line
3. This is Third line
4. This is Fourth line
5. This is Fifth line
6. This is Sixth line
7. This is Seventh line
8. This is Eighth line
9. This is Nineth line
10. This is Tenth line
11. This is Eleventh line
12. This is twelveth line
PS C:\>  

In the above PowerShell command, using Get-Content cmdlet parameter get the content of file and pass it as input to next command over pipe operator (|).

Select -Skip 1 command skip the first line of file and display multiple lines available in file.

Output of Get-Content Skip first line of file and display rest of multiple lines.

Get Specific Lines from Text File

Sometime, it is require to get specific lines from text file to get content for further operation. To get 10th line of file using PowerShell , use below command

PS C:\> Get-Content D:\MultipleLineExamples.txt | Select -First 10 | Select -Last 1                                     

10. This is Tenth line
PS C:\>   

In the above example,

  • Get-Content get content of file which has multiple lines in it.
  • Using Select -First 10 command, it get first 10 lines of file.
  • Using Select -Last 1 command, it get last line of file, which is 10th line of file.

Cool Tip: Learn how to get aduser using userprincipalname!

Conclusion

In the above blog post, I have explained how to get first line of the file using Get-Content command. First parameter is very useful to get specific number of lines from the file and display top line of file.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.

Leave a Comment