How to read a text file in PowerShell quickly and easily

Text files are ubiquitous and you’ll have to read through them, regardless of your role or job responsibilities. In fact, as an IT professional, you will have to create, read, or work with text files more often than you may think because text files come in a wide range of formats. This flexibility has also made text files the most convenient way to define scripts, store configuration details, and more. PowerShell understands this importance of text files and makes it easy to retrieve text and read from them easily. In this article, we’ll see how you can read a text file in PowerShell.

Reading the entire contents

linux text editors
Shutterstock

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function.

Here is the code that allows you to do this:

Get-Content C:\logs\log01012020.txt

When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.

You can also move all the content to a variable and use that variable for further processing if that’s something that you want your code to do.

$file_data = Get-Content C:\logs\log01012020.txt

You can now use this variable $file_data for parsing or further processing.

Reading partial content

powershell-text-files

Many times, you may only have to read a certain part of a file to get the information you want. Similar to a SQL query, you can choose the lines you want to read and the code for that is:

$file_data = Get-Content C:\logs\log01012020.txt
$file_data | Select-Object -First 10

As the code suggests, the first 10 lines of the content will be stored in the variable and not the entire content. You can display the contents of this variable or use it for more processing.

Likewise, we can read the last few lines too:

$file_data = Get-Content C:\logs\log01012020.txt
$file_data | Select-Object -Last 10

Reading line-by-line

When you want to read the file to understand its contents, you’d have to do so one line at a time and the good news is, this is possible with PowerShell.

In fact, there are two ways to do it.

Using Get-Content

The Get-Content function reads every line in the text and stores them as an array, where each line is an array element. In the above example, we used a variable for reading all the content.

$file_data = Get-Content C:\logs\log01012020.txt

If you print the type of this variable, you can see that it an array.

This means you can pick out specific lines using the array index. For example, if you want to read the first line, you can simply say:

$file_data[0]

And this will display the first line for you.

$file_data[1]

This will display the second line, and so on.

Using StreamReader class

The second option is to use a .NET class called StreamReader.

$stream_reader = New-Object System.IO.StreamReader{C:\logs\log01012020.txt}

Now you have the contents of the log file in this $stream_reader variable and since it belongs to the StreamReader class, you can use many built-in methods to get the text you want.

When you say…

$stream_reader.ReadToEnd()

…this will output all the contents on your screen, similar to Get-Content.

To read the current line, you can use the following method:

$stream_reader.ReadLine()

But this alone may not be helpful, so you’ll have to run it in a loop and read the contents of a text file, line by line.

$stream_reader = New-Object System.IO.StreamReader{C:\logs\log01012020.txt}
$line_number = 1
while (($current_line =$stream_reader.ReadLine()) -ne $null)
{
Write-Host "$line_number  $current_line"
$line_number++
}

The above code snippet will start with the first line and will print out each line with its line number for easy reading. You can even use any of the available string methods on the $current_line variable to parse it further.

This method is ideal for reading large files because when you store the contents in a variable, it can take up too much memory and can impact performance as well. So, this is a more efficient way to get to the content you want.

Finding specific text

Many times, you’d want to find a specific text in a file and the best to do that is to use the Where-Object cmdlet that filters the content to give you what you want.

$file_data = Get-Content C:\logs\log01012020.txt | Where-Object {$_ -like ‘*error*’}

The above code will output those lines that have the word “error” in them. Here, the $_ is a pipeline variable that represents the current line from the contents that come from Get-Content.

Besides Where-Object, you can also use match and regex operators to find the exact text you want.

More options with Get-Content

Get-Content is a highly versatile cmdlet that comes loaded with many options. Here are some things you can do with it.

Count the number of lines in the file

You may want to know the number of lines available in the file, so you can parse it accordingly. The code for that is:

$file_data = Get-Content C:\logs\log01012020.txt | Measure-Object

Specific number of lines at the beginning and the end

Earlier, we saw how to choose the first few or last few lines using the Select-Object cmdlet. You can also get the same results using some of the built-in methods of Get-Content.

To get the first few lines, the method is:

$file_data = Get-Content C:\logs\log01012020.txt -TotalCount 3

This will return the first three lines from the file.

$file_data = Get-Content C:\logs\log01012020.txt -Tail 3

This command will return the last three lines from the file.

Continuously updated file

Let’s say your log file is continuously updated and you want to see the end of the log file to read the last updated values. For that, you can add the Wait parameter, like this:

$file_data = Get-Content C:\logs\log01012020.txt -Tail 3 -Wait

This command will continuously monitor the log file for the new lines that are getting added to it and will display the same to you.

Thus, these are some of the different ways to read a text file in PowerShell. Do let us know if you know more ways to read the contents of a file in PowerShell.

Featured image: Freerange Stock

About The Author

1 thought on “How to read a text file in PowerShell quickly and easily”

  1. The variables are NOT necessary in most of the examples.

    The only reason to have a variable is for accessing the same information more than once.

    Ad hoc coding rarely requires variables.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top