Read file line by line in PowerShell Read file line by line in PowerShell powershell powershell

Read file line by line in PowerShell


Not much documentation on PowerShell loops.

Documentation on loops in PowerShell is plentiful, and you might want to check out the following help topics: about_For, about_ForEach, about_Do, about_While.

foreach($line in Get-Content .\file.txt) {    if($line -match $regex){        # Work here    }}

Another idiomatic PowerShell solution to your problem is to pipe the lines of the text file to the ForEach-Object cmdlet:

Get-Content .\file.txt | ForEach-Object {    if($_ -match $regex){        # Work here    }}

Instead of regex matching inside the loop, you could pipe the lines through Where-Object to filter just those you're interested in:

Get-Content .\file.txt | Where-Object {$_ -match $regex} | ForEach-Object {    # Work here}


Get-Content has bad performance; it tries to read the file into memory all at once.

C# (.NET) file reader reads each line one by one

Best Performace

foreach($line in [System.IO.File]::ReadLines("C:\path\to\file.txt")){       $line}

Or slightly less performant

[System.IO.File]::ReadLines("C:\path\to\file.txt") | ForEach-Object {       $_}

The foreach statement will likely be slightly faster than ForEach-Object (see comments below for more information).


The almighty switch works well here:

'onetwothree' > file$regex = '^t'switch -regex -file file {   $regex { "line is $_" } }

Output:

line is twoline is three