Often you may want to use PowerShell to select a specific range of lines from a file.
You can use the following basic syntax to do so:
Get-Content teams.txt | Select-Object -Index (3..6)
This particular example will return only lines in index positions 3 through 6 of the file named teams.txt.
Note: The first line in a file has index position 0.
Here is what the various commands do:
- Get-Content: This retrieves all of the lines from a specific file
- Select-Object: This selects specific lines from the file
- -Index (3..6): This selects lines with index positions 3 through ( .. ) 6
The following examples show how to use this syntax in practice.
Related: How to Get Last N Lines of a File in PowerShell
Example: How to Select Range of Lines from File in PowerShell
Suppose we have a file named teams.txt that contains information about various basketball teams.
We can use the Get-Content cmdlet to first view all of the lines in this file:

Now suppose that we would like to only select the lines in index positions 3 through 6 in the file.
We can use the following syntax to do so:
Get-Content teams.txt | Select-Object -Index (3..6)
The following screenshot shows how to use this syntax in practice:

Notice that this returns only lines in index positions 3 through 6, just as we specified.
Feel free to replace 3 and 6 with whatever numbers you would like to select a different specific range of lines from your own file.
Note: You can find the complete documentation for the Select-Object cmdlet in PowerShell here.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Find Duplicate Lines in File
PowerShell: How to Remove Empty Lines from a File
PowerShell: How to Print N Lines After Matching Pattern
