Stream a Text File Line by Line in PowerShell
PowerShell
function Read-FileLine {
param(
[Parameter(Mandatory)][string]$Path,
[scriptblock]$OnLine = { param($line) $line }
)
$count = 0
foreach ($line in [System.IO.File]::ReadLines($Path)) { # lazy, never loads the whole file
if ([string]::IsNullOrWhiteSpace($line)) { continue }
& $OnLine $line
$count++
}
Write-Verbose "Read $count non-empty lines from $Path"
}