Use the Get-Content
cmdlet in PowerShell to read the file, and get the content of a text or CSV file. It has the First parameter to select first line of file or specific lines from a file. The Skip
parameter skips the number of lines from a file.
Very often Administrators has a task to monitor log files like text file, CSV file, windows log file, IIS log, error log, and 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 the Get-Content cmdlet and the First parameter with value to get specific lines from a text file.
Using PowerShell script, it’s very easy and quick to display the top lines of a text file or CSV file. The First parameter specifies the number of lines from the start at the line of a file.
Let’s understand with examples of using the Get-Content cmdlet to get first 10 lines of file, a file can be a text file, CSV file, or log file.
Cool Tip: How to get certificates using PowerShell!
PowerShell to Get First Line of File
Use the Get-Content cmdlet with the First parameter in PowerShell to get first line of file. The command reads the first line of file and using Select -First 1 gets the first line of the file.
PS C:\> Get-Content D:\MultipleLineExamples.txt | Select -First 1 1. This is First line PS C:\>
In the above script in PowerShell, to get line from the file
- The Get-Content command reads the content of the text file specified by the path
- Pass through the content of a file to the pipe operator (|).
- The pipe Operator passes it as input to read the first line of the file using
Select -First 1
PowerShell Get-Content First 10 lines of File
In PowerShell to select the first 10 lines of file, use the Get-Content command with Select -First parameter with value 10. Here -First 10
selects the 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 reads the content of the file and passes through the pipeline operator to the Select -First 10
to select the first 10 lines of file.
Cool Tip: Do you know the equivalent of the cat command in Windows?
PowerShell Get-Content Skip first line
In PowerShell, using Get-Content to skip first line of file, use the Skip parameter with value to skip the line. It will skip those lines and display the rest of the lines of the 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 the Get-Content cmdlet parameter get the content of a file and pass it as input to the next command over the pipe operator (|).
Select -Skip 1
command skip the first line of file and display multiple lines available in file.
The output of Get-Content skips first line of file and displays the rest of the multiple lines.
Get Specific Lines from Text File
Sometimes, it is required to get specific lines from a text file for further operation. To get the 10th line of file using PowerShell, use the 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 gets the content of a file that has multiple lines in it.
- Using the Select -First 10 command, it get first 10 lines of file.
- Using the Select -Last 1 command, it gets the last line of file, which is the 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 the Get-Content command. The 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 the ShellGeek home page.