- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve a specific number of lines from files in PowerShell?
To retrieve the specific number of lines from the beginning or the end of the file, you first need to get the content of the file using Get-Content and then need to pipeline the -First for retrieving the number of files from the beginning and -Last to retrieve the number of lines from the bottom.
Check the below example of retrieving the content of the first 10 lines.
Example
Get-Content D:\Temp\PowerShellaliases.txt -First 10
Output
PS C:\WINDOWS\system32> Get-Content D:\Temp\PowerShellaliases.txt -First 10 CommandType Name Version Source ----------- ---- ------- ------ Alias % -> ForEach-Object Alias ? -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias cat -> Get-Content Alias cd -> Set-Location Alias CFS -> ConvertFrom-String 3.1.0.0 Microsoft.PowerShell.Utility
To get the content of the last 10 lines from the file.
Example
Get-Content D:\Temp\PowerShellaliases.txt -Last 10
Output
PS C:\WINDOWS\system32> Get-Content D:\Temp\PowerShellaliases.txt -Last 10 Alias swmi -> Set-WmiInstance Alias tee -> Tee-Object Alias trcm -> Trace-Command Alias type -> Get-Content Alias wget -> Invoke-WebRequest Alias where -> Where-Object Alias wjb -> Wait-Job Alias write -> Write-Output
- Related Articles
- How to retrieve files and folders attributes using PowerShell?
- How to copy files of the specific extension using PowerShell?
- How to copy files with the specific extension in PowerShell?
- How to retrieve specific file(s) information using Get-ChildItem in PowerShell?
- How to remove empty string/lines from PowerShell?
- How to count the total number of lines in the file in PowerShell?
- How to retrieve the content of the file in PowerShell?
- How to retrieve Azure VMs using PowerShell?
- How to retrieve certificate thumbprint using PowerShell?
- How to retrieve tasks in Task scheduler using PowerShell?
- How to display files/folders including hidden files/folders in PowerShell?
- How to display specific properties from Get-Service output in PowerShell?
- How to merge lines of files in the Linux system?
- How to sort lines of text files in Linux?\n
- How to delete all users from specific OU using PowerShell?

Advertisements