How to Read a Text File in PowerShell and Skip the First Line?

Recently, I received a requirement to read a text file in PowerShell but skip the first line. This is a very common requirement for a PowerShell developer. In this tutorial, I will explain how to read a text file in PowerShell and skip the first line using various methods with examples.

I will use the text file below for all the examples. You can copy and save this content as a .txt file on your drive.

State,Population
California,39538223
Texas,29145505
Florida,21538187
New York,20201249
Pennsylvania,13002700
Illinois,12812508
Ohio,11799448
Georgia,10711908
North Carolina,10439388
Michigan,10077331

Read a Text File in PowerShell and Skip the First Line

The best way to read a text file in PowerShell is by using the Get-Content cmdlet. This cmdlet retrieves the content of a file and returns it as an array of strings, where each element represents a line in the file. To skip the first line, we can combine Get-Content with other cmdlets or use its built-in parameters.

Let me show you how to do this with examples.

Method 1: Skipping the First Line with Select-Object

One approach to skip the first line of a text file is by using the Select-Object cmdlet in conjunction with Get-Content in PowerShell. Here’s an example:

Get-Content -Path "C:\Bijay\data.txt" | Select-Object -Skip 1

In this code snippet, we use Get-Content to read the content of the file located at "C:\Bijay\data.txt". The output is then piped (|) to the Select-Object cmdlet, where we specify the -Skip parameter with a value of 1. This instructs PowerShell to skip the first line and return the remaining lines.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Read a Text File in PowerShell and Skip the First Line

Check out Remove the Last Line from a File Using PowerShell

Method 2: Skipping the First Line with Array Indexing

Another way to skip the first line is by leveraging PowerShell’s array indexing capabilities. Here’s an example:

(Get-Content -Path "C:\Bijay\data.txt")[1..(Get-Content -Path "C:\Bijay\data.txt").Count]

In this approach, we use parentheses to wrap the Get-Content cmdlet, which returns an array of strings. We then use array indexing to select a range of elements starting from index 1 (skipping the first line) up to the total count of lines in the file. The .Count property returns the number of elements in the array.

Here is the exact output in the screenshot below:

How to Read a Text File in PowerShell and Skip the First Line

Check out Remove the Last Empty Line from a File Using PowerShell

Using the ReadLine() Method

Another method to read a text file and skip the first line in PowerShell is by using the ReadLine() method of the StreamReader class. This approach provides more control over the reading process and allows for line-by-line processing.

$filePath = "C:\Bijay\data.txt"
$reader = New-Object System.IO.StreamReader($filePath)
$reader.ReadLine() | Out-Null  # Skip the first line
while (($line = $reader.ReadLine()) -ne $null) {
    # Process each line
    Write-Output $line
}
$reader.Close()

In this code snippet, we create a StreamReader object using the New-Object cmdlet and specify the file path. We then use the ReadLine() method to read the first line and pipe it to Out-Null to discard it.

Next, we enter a while loop that continues reading lines until the end of the file is reached. Inside the loop, we can process each line as needed. Finally, we close the StreamReader object to release system resources.

Check out Remove Blank Lines from CSV Files Using PowerShell

Skipping Multiple Lines While Reading Text Files Using PowerShell

You can easily modify the previous methods to accommodate your requirements if you need to skip multiple lines instead of just the first line. Here are a few examples:

Skipping the First N Lines with Select-Object

To skip the first N lines using Select-Object, simply adjust the value of the -Skip parameter:

$linesToSkip = 3
Get-Content -Path "C:\Bijay\data.txt" | Select-Object -Skip $linesToSkip

In this example, we specify the number of lines to skip using the $linesToSkip variable and pass it to the -Skip parameter. PowerShell will then return the content of the file starting from line N+1.

Skipping the First N Lines with Array Indexing

Similarly, you can modify the array indexing approach to skip the first N lines:

$linesToSkip = 3
(Get-Content -Path "C:\Bijay\data.txt")[($linesToSkip)..(Get-Content -Path "C:\Bijay\data.txt").Count]

Here, we use the $linesToSkip variable to specify the starting index for the range selection. PowerShell will return the content of the file starting from line N+1 up to the end of the file.

Check out Remove Blank Lines from PowerShell Format-Table Output

Skipping the First N Lines with ReadLine()

When using the ReadLine() method, you can modify the code to skip the first N lines by calling ReadLine() N times before entering the processing loop:

$filePath = "C:\Bijay\data.txt"
$linesToSkip = 3
$reader = New-Object System.IO.StreamReader($filePath)
for ($i = 0; $i -lt $linesToSkip; $i++) {
    $reader.ReadLine() | Out-Null  # Skip the first N lines
}
while (($line = $reader.ReadLine()) -ne $null) {
    # Process each line
    Write-Output $line
}
$reader.Close()

In this code snippet, we introduce a for loop that iterates N times, where N is determined by the $linesToSkip variable. During each iteration, we call ReadLine() to read a line and pipe it to Out-Null to discard it. After skipping the first N lines, we proceed with the processing loop as before.

Check out Remove the First and Last Lines from a File Using PowerShell

Handle Different File Types

The methods discussed so far work well with plain text files, but what if you need to read and skip lines in files with different formats, such as CSV or TSV? PowerShell provides specific cmdlets to handle these file types efficiently.

Reading CSV Files

To read a CSV file and skip the first line, you can use the Import-Csv cmdlet:

Import-Csv -Path "C:\Bijay\data.csv" | Select-Object -Skip 1

The Import-Csv cmdlet automatically parses the CSV file and creates objects based on the headers. By piping the output to Select-Object -Skip 1, we can easily skip the first line (header row) and work with the remaining data.

Reading TSV Files

For TSV (Tab-Separated Values) files, you can use the Import-Csv cmdlet with the -Delimiter parameter set to a tab character:

Import-Csv -Path "C:\Bijay\data.tsv" -Delimiter "`t" | Select-Object -Skip 1

The -Delimiter parameter specifies the character used to separate the values in the file. By using a tab character ("t”), PowerShell can correctly parse the TSV file and create objects. Again, we can skip the first line usingSelect-Object -Skip 1`.

Conclusion

In this tutorial, we explored various methods to read a text file in PowerShell and skip the first line, such as using the Get-Content cmdlet with Select-Object or array indexing, or leveraging the ReadLine() method of the StreamReader class, etc.

Additionally, we discussed how to skip multiple lines and work with different file formats like CSV and TSV.

I hope this tutorial has been helpful to you. Do let me know in the comment if you still have any questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.