Powershell

Read file line by line in PowerShell

PowerShell is a Windows utility used to manage and automate all the administrative tasks in Windows. It is also beneficial when it comes to managing files, such as creating files, deleting files, moving files, or reading files. If you have a text file and are having difficulties reading line by line in Notepad, then let PowerShell do it for you.

This post will overview a complete guide to resolve the stated query.

How to Read a File Line by Line in PowerShell?

The files in PowerShell can be read using the given methods:

Method 1: Reading a File Line by Line Using “Get-Content” Cmdlet

PowerShell uses the “Get-Content” cmdlet to get the file’s content, such as the content of the text file. In this approach, the “ForEach” loop is used to iterate through the text file and read the file line by line.

Example

In this given example code, we will use the “Get-Content” cmdlet with the help of the “foreach” loop to read a file line by line:

$FILE = Get-Content "C:\Users\Muhammad Farhan\Desktop\new.txt"

foreach ($LINE in $FILE)

{

Write-Output "$LINE"

}

Here:

  • Get-Content” cmdlet is used to fetch/retrieve the file from the specified location.
  • foreach()” loop is utilized in the above code to read the file line by line.

Output

The output confirms that the file has been read line by line.

Method 2: Reading a File Line by Line Using “[System.IO.File]” Class

Another method that can be used as the replacement for the Get-Content cmdlet is the .NET library’s “[System.IO.File]” class. It also assists in getting the content of the file using the “ReadLines()” method.

Example

We will now use the “[System.IO.File]” class with the combination of the “foreach” loop to read the file line by line:

foreach($line in [System.IO.File]::ReadLines("C:\Users\Muhammad Farhan\Desktop\new.txt"))
{
    Write-Output $line
}

Here:

  • [System.IO.File]” class uses the “ReadLines()” parameter to read a file.
  • foreach” loop is added to iterate through files line by line.

Output

Method 3: Reading a File Line by Line Using “RegEx” Class

The “regex” is the short form of the “Regular Expression”. It is a pattern utilized to match the text in the corresponding file. Moreover, it can be also utilized to read a file line by line. To do so, specify the value of the “$regex” variable as an empty string and use the “foreach” loop to read the file sequentially.

Example

Now, we will use the “RegEx” with the combination of the “Get-Content” cmdlet and “foreach()” loop to read a file line by line:

$regex = ''
foreach($line in Get-Content -Path "C:\Users\Muhammad Farhan\Desktop\new.txt")
{
    if($line -match $regex)
    {
        Write-Output $line
    }
}

In the above code:

  • $regex” variable contains empty string as a regular expression.
  • Get-Content” cmdlet is used to retrieve the file and content inside of it.
  • foreach” is used to iterate through the lines.

Output

The output verifies that the file was read line by line.

Conclusion

To read files line by line in PowerShell, use the combination of the “Get-Content” cmdlet and “foreach” loop. The Get-Content cmdlet gets the content of the file, and the foreach loop will iterate through lines to help read the code line by line. Moreover, “regex” and “[System.IO.File]” methods can be also used for the same purpose. This post has presented numerous approaches to resolve the mentioned query.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.