How to Read the First Line of a File in PowerShell?

A common task when working with text files in PowerShell is reading the first line of a file. PowerShell provides several ways to get the first line of a file, and in this blog post, we will explore different methods to read the first line of a file using PowerShell.

To read the first line of a file in PowerShell, you can use the Get-Content cmdlet combined with Select-Object -First 1. Alternatively, you can use array indexing on the result of Get-Content, or use the -TotalCount parameter to limit the output to the first line only. For large files, consider using a StreamReader to efficiently read the first line without loading the entire file into memory.

1. Using Get-Content with Select-Object

One of the simplest ways to read the first line of a file in PowerShell is by using the Get-Content cmdlet combined with Select-Object. The Get-Content cmdlet reads the file and returns its content while Select-Object can be used to select the first line.

Here’s an example and the PowerShell script.

$firstLine = Get-Content -Path "C:\MyFolder\MyFile.txt" | Select-Object -First 1
Write-Host "The first line is: $firstLine"

This script reads the first line of the specified file and prints it to the console.

You can see in the screenshot below I executed the PowerShell script using VS code.

Read the First Line of a File in PowerShell

2. Using Get-Content with Array Indexing

Another way to access the first line from a file in PowerShell is by treating the output of Get-Content as an array and then accessing the first element using array indexing.

Here is a complete example.

$content = Get-Content -Path "C:\MyFolder\MyFile.txt"
$firstLine = $content[0]
Write-Host "The first line is: $firstLine"

When Get-Content reads a file, each line is treated as an element in an array. By accessing the index [0], you can retrieve the first line.

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

How to Read the First Line of a File in PowerShell

3. Using the -TotalCount Parameter

The Get-Content cmdlet in PowerShell has a parameter called -TotalCount that allows you to specify the number of lines to read from the beginning of a file. To read only the first line, you can set -TotalCount to 1.

Example:

$firstLine = Get-Content -Path "C:\MyFolder\MyFile.txt" -TotalCount 1
Write-Host "The first line is: $firstLine"

This method is efficient because it instructs PowerShell to stop reading the file after the first line, which can save time and resources when dealing with large files.

4. Using the Select-String Cmdlet

You can also use the Select-String cmdlet to read the first line of a file in PowerShell. This cmdlet is typically used for pattern matching, but it can also be used to grab content from a file.

Example:

$firstLine = Select-String -Path "C:\MyFolder\MyFile.txt" -Pattern "." -List | Select-Object -First 1
Write-Host "The first line is: $firstLine"

This script uses Select-String to find the first instance of any character (using the regex pattern “.”) and then pipes the result to Select-Object to grab the first match.

5. Using StreamReader for Large Files

If you are working with very large files, using Get-Content might not be the most efficient approach due to memory usage. In such cases, you can use the [System.IO.StreamReader] class to read the file line by line.

Example:

$streamReader = [System.IO.StreamReader] "C:\path\to\your\file.txt"
$firstLine = $streamReader.ReadLine()
$streamReader.Close()
Write-Host "The first line is: $firstLine"

This script creates a StreamReader object, reads the first line of the file, closes the stream, and then prints the line to the console.

Conclusion

In PowerShell, reading the first line of a file can be accomplished in several ways like Get-Content with Select-Object, array indexing, the -TotalCount parameter, Select-String, or a StreamReader etc.

The best way to read the first line of a file in PowerShell is to use the Get-Content with Select-Object cmdlets. I have explained all the methods with examples.

You may also like: