Read Text File in PowerShell

Powershell as the more powerful alternative to CMD offers a lot of features to manipulate text files. In this article, I’ll show you multiple ways on how you can read text files.

Read the whole file

The simplest way to read the whole file in PowerShell:

Get-Content file.txt

Where “file.txt” is the name of the file.

Windows will display all lines from the text file inside the PowerShell console.

The Get-content cmdlet gets the content of a file at the location specified in the command.

Read the limited number of lines

Read several lines from the beginning

Reading the whole file, especially if it contains many lines, is not always the best option. You can limit the number of lines with the TotalCount parameter.

This command displays the first three lines of a file.

Get-Content file.txt -TotalCount 3

You can also read the first number of lines using the First parameter.

Get-Content file.txt -First 3

This comment will read a few lines from the beginning to the number specified.

Read several lines from the end

Similarly, you can read several lines from the end of a file. Instead of the First parameter, use Last.

Get-Content file.txt -Last 3

This code returns the last three lines from the “file.txt” file. In this case, the command works the same way as the TotalCount from the previous example.

To get the same result, you can use Tail, instead of Last.

Get-Content file.txt -Tail 3

Read the specific line

The following code reads and displays line 6.

(Get-Content file.txt -TotalCount 6)[-1]

This command takes 6 lines from a file and displays only the last one [-1], which is line 6.

If you modify this example, and instead of [-1] use [-2] the program will retrieve 6 lines and return the second from the end – which is the 5th line.

Count the number of lines in the file

Measure-Object returns the number of lines in a file. Thanks to this cmdlet, you have information that allows you to parse your document accordingly.

It looks like this:

Get-Content file.txt | Measure-Object

The count is 10. That means that we have 10 lines inside our file.

The Wait parameter

The Tail parameter is often used in tandem with the Wait parameter. It’s useful when you have to read files that are constantly updated; for example, log files.

This parameter will monitor the file and will display every new line that is added.

Open “file.txt” and insert the new line “New line” at the end of the file, then save it.

Add the next line: “And another one” and save the file.

Messages inside PowerShell are refreshed in real-time. The new line character is added after each save. If we add these two lines and save it, the result will look as follows:

How to find the specific Text

In PowerShell, you can filter various information from a file and display them inside the console. In this example, we are going to use a popular Where-Object cmdlet and display only those lines from the file that contains the specific text.

Get-Content file.txt | Where-Object {$_ -like '*1*'}

This code displays only lines that have the number 1 inside.

With the Where-Object parameter, there is a special PowerShell variable: $_. It is known as the pipeline variable, and it represents each line from the text file.

The asterisk (*) is a wildcard character and represents a string of characters from 0 to infinite. This is going to be interpreted as a string that has “1” inside and any number of characters before or after the character.

Move the content to a variable

So far, I showed you how to read files and display the result on the console. In PowerShell, you can save the result to a variable, instead of displaying it.

Try to run this command:

$file = Get-Content file.txt

It won’t display anything, but the result will be saved to the $file variable.

Now you can use this variable to display the whole file.

You can also display single lines. There are 10 lines inside the variable. Index starts from 0. That means that the first line is $file[0] and the last one is $file[9].

You can also display multiple single lines at once.

You can also display ranges of lines.

You can display ranges in reverse order.

Using the StreamReader class

There is an option to use StreamReader – a .NET class.

To open a file and save it to a variable, use this command:

$sr = New-Object System.IO.StreamReader{D:\file.txt}

Remember to write the full path to a file. If you use only a file name, PowerShell will use the default path, instead of the current path.

Use this command to display all the lines from the beginning to the end:

$sr.ReadToEnd()

The StreamReader class also has the ReadLine function that reads and displays the current line and moves to the next.

$sr.ReadLine()

Now, take a look what happens, if you run this code:

$sr = New-Object System.IO.StreamReader{D:\file.txt}
$sr.ReadLine()
$sr.ReadLine()
$sr.ReadLine()
$sr.ReadToEnd()

Each time you run $sr.ReadLine(), you move the cursor to the next line. If you run $sr.ReadToEnd(), the function will read the remaining lines to the end of the file.

You can also add a string at the end of the line with the ReadLine function, but if you want to add text to each line, you can use this function as many times as there are lines, but you can’t do this at once with the ReadToEnd function.

As you can see, PowerShell doesn’t add text to each line when you use the ReadToEnd function, but only to the very last.

Read text file line by line with loops

If you want to add text to each line, but you don’t want to use a command for each line, you can use loops. Loops are used in programming languages to run the code multiple times.

This code will display each line with the ” <end line>” string at the end.

$sr = New-Object System.IO.StreamReader{D:\file.txt}
$counter = 1
while (($current_line = $sr.ReadLine()) -ne $null)
{
$current_line + " <end line>"
$counter++
}

Close StreamReader

After you’ve done operations on the StreamReader variable, you have to close it.

$sr.Close()