PowerShell – Read File Line by Line

PowerShell - read file line by line

Using Get-Content Cmdlet

Use the Get-Content cmdlet to read file line by line in PowerShell.

We used the Get-Content cmdlet to read the specified file line by line in PowerShell. It is used to get the content or data of the specified item; here, the specified item can be a text file or function.

For files, the Get-Content cmdlet will read one line at a time and return the collection of objects where every object will represent the line of content.

If you are using PowerShell version 3.0 or above, you can also get a particular line number from the start or end of the file. We can also get the same results as above using Get-Content with ForEach().

In the above code, we got all the content of the specified file using Get-Content and then used the ForEach() loop to iterate over each line of the retrieved content. For each iteration, we used echo to print every line.

Usually, this approach (using Get-Content with ForEach()) is preferred where we need to manipulate every line. For example, we will use the concatenation operator (+) in the echo statement as follows if we want to add OK - at the start of every line and then display it.

Now, think of a situation where we need to read a specified file line by line, but for a specific number of lines from the start/end of the given text file, the following solution will work for us.

Use Get-Content Cmdlet to Read Partial Data

To read a specified text file line by line in PowerShell:

  • Use Get-Content to read the specified file line by line.
  • Use Select-Object with the -First/-Last parameter name to read the first/last n number of lines.

In the previous section, we learned about the Get-Content cmdlet. Here, we used it again to read content from the specified file, but this time, we saved the read data in the $file_content variable.

Next, we forwarded the value of the $first_content variable to the Select-Object cmdlet using pipe (|), which is used to forward data from the previous process to the following process.

The Select-Object cmdlet is used to select object/object properties. We can also use it to choose a particular number of unique objects and objects from a specific position in an array.

This cmdlet allows us to use various parameters, for instance, -First and -Last, that select a particular number of objects from an array.

In our case, each line is an object and -First selects the first four lines and returns them as an array. The -Last performs the same but selects objects from the last.

The following solution is for us if we are looking for a way to read the whole file line by line but display those which match the particular pattern. Let’s learn this approach below.

Use Get-Content Cmdlet with -Like Operator

To read the given file line by line in PowerShell:

  • After defining our pattern, use ForEach() to iterate over each line of a file that we read using the Get-Content cmdlet.
  • In each iteration, use the if statement with the -Like operator to search the specified pattern in the current line.
  • Within the if block, use the Write-Output cmdlet to print the current line if the if condition is fulfilled.

This code example is similar to the first section, where we used Get-Content with the ForEach() loop. The only difference is to use the if statement with the -Like operator to search for the given pattern and display the $current_line if it fulfilled the if condition.

The advantage of using the -Like operator is that we do not need to escape the backslash. Now, what pattern are we using? We printed only those lines on PowerShell Console if they ended with any number immediately followed by a dot (.).

Using switch Statement

Use switch to read the given text file line by line in PowerShell.

We used the switch with -Regex and -File parameters, where -File was used to specify the file name for reading content and -Regex was used to perform regex matching of a value to the condition.

In the above code example, we have written a custom regex value as I, ensuring that the matching line starts with I. If the specified regex condition is evaluated as Trues, it would print the lines as shown in the output given above.

If you are wondering about $_, do not worry; it is an automatic variable in PowerShell that holds the line for which the regex condition is satisfied.

Using the ReadLines() of [System.IO.File] Class

Use the ReadLines() method of the [System.IO.File] class to read the specified file line by line in PowerShell.

Using the ReadLines() method of the [System.IO.File] class is an excellent alternative to the Get-Content cmdlet to read the text file (located in the given path) line by line.

Until now, we have covered various ways to read a file line by line, but what if we have any empty lines? Let’s see the following approach for learning how to handle blank lines.

Using the StreamReader Class

To read a file line by line in PowerShell:

  • Create a StreamReader object using the New-Object cmdlet.
  • Use the while loop to read one line at a time and check if it is not empty.
  • If the current line is not blank, print the line number along with the line’s text using the Write-Host cmdlet.

Here, we created a StreamReader object using New-Object cmdlet and saved it in $stream_reader variable. Next, we declared a $line_number and initialized it with 1; we will use this variable to maintain the line numbers while reading.

Next, we used the .ReadLine() method of the $stream_reader object to read the specified file line by line; on each iteration, the $current_line variable will be updated with the text returned by .ReadLine() method. We can only move inside the while if the $current_line is not equal to an empty line.

If it is not an empty line, print the $line_number along with $current_line and increment the $line_number by 1; otherwise, exit from the while loop. Remember, this loop will exist as soon as it finds an empty line. You must update the code according to your needs if you want to read all files, excluding blank lines.

You can also remove empty lines from file if you don’t them and read the file line by line by above methods.

That’s all about how to read file line by line in PowerShell.

Was this post helpful?

Leave a Reply

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