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 how you can read text files.

Read the whole file

The simplest way to read the whole file in PowerShell:

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.

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.

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.

Read the specific line

The following code reads and displays line 6.

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:

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 saves. 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.

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:

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:

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:

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

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

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.

Close StreamReader

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