In PowerShell, recently, I got a requirement to read a text file line by line. There are various methods to do this. In this PowerShell tutorial, I will explain several methods to read a file line by line using PowerShell with examples and full scripts.
To read a file line by line in PowerShell, you can use the Get-Content cmdlet combined with a foreach loop, which reads the file content and iterates through each line. For large files, consider using the [System.IO.File]::ReadLines method or a [System.IO.StreamReader] object to read lines sequentially without loading the entire file into memory, thus conserving system resources. Here’s a concise example using Get-Content:
Get-Content -Path "C:\MyFolder\file.txt" | foreach { Write-Output $_ }Method-1: Using Get-Content and a Foreach Loop
The most straightforward way to read a file line by line in PowerShell is to use the Get-Content cmdlet in combination with a foreach loop.
The PowerShell Get-Content cmdlet reads the content of a file and returns it line by line by default.
Here’s a simple example:
$fileContent = Get-Content -Path "C:\MyFolder\file.txt"
foreach ($line in $fileContent) {
# Process each line
Write-Output $line
}In this script, Get-Content retrieves each line of file.txt and stores it in the $fileContent variable. The foreach loop then iterates over each line, and Write-Output displays it on the console.
Once you execute the PowerShell script, you can see the output like in the screenshot below:

Method-2: Streaming File Content
If you’re working with large files in PowerShell, you might want to stream the file content to avoid loading the entire file into memory. This can be done by piping the output of Get-Content directly into a foreach loop:
Get-Content -Path "C:\MyFolder\largefile.txt" -Stream | foreach {
# Process each line
Write-Output $_
}Here, $_ represents the current line in the pipeline, allowing you to work with it directly without storing the entire file content in a variable.
Method 3: Using ForEach-Object Cmdlet
Another approach involves piping the output of Get-Content directly into ForEach-Object, which allows you to process each line within the pipeline in PowerShell:
Get-Content -Path "C:\MyFolder\file.txt" | ForEach-Object {
# Process each line
Write-Host $_
}In this example, $_ represents the current object in the pipeline, which is the current line being processed.
Here is the output:

Method 4: Using [System.IO.File]::ReadLines()
For large files, it’s more efficient to use the [System.IO.File]::ReadLines() method, which reads the file lazily, meaning it only reads lines into memory as they are needed.
Example:
[System.IO.File]::ReadLines("C:\path\to\your\file.txt") | ForEach-Object {
# Process each line
Write-Host $_
}This method is particularly useful for processing very large files without consuming a lot of memory.
Method 5: Using Stream Reader
If you need more control over the file reading process, you can use a StreamReader object in PowerShell. This is also a good option for large files.
Example:
$streamReader = [System.IO.StreamReader] "C:\MyFolder\file.txt"
try {
while ($streamReader.Peek() -ge 0) {
$line = $streamReader.ReadLine()
# Process each line
Write-Host $line
}
}
finally {
$streamReader.Close()
}This script creates a StreamReader object for the file and reads each line in a while loop until there are no more lines to read. It’s important to close the StreamReader with the Close() method when done to free up resources.
Method 6: Using Switch Statement
PowerShell’s switch statement can also be used to read a file line by line.
Here is a simple Example:
switch -File "C:\MyFolder\file.txt" {
default {
# Process each line
Write-Host $_
}
}The switch statement reads the file and processes each line as it goes, using $_ to reference the current line.
Real Example
Here’s a complete script that reads a log file and extracts lines that contain the word “Error” in PowerShell:
$logFilePath = "C:\MyFolder\log.txt"
$errorLines = @()
Get-Content -Path $logFilePath | ForEach-Object {
if ($_ -match "Error") {
$errorLines += $_
}
}
# Output error lines to a new file
$errorLogPath = "C:\NewFolder\error_log.txt"
$errorLines | Out-File -FilePath $errorLogPath
Write-Host "Error lines extracted to $errorLogPath"This script reads a log file, filters out lines that contain “Error”, and writes those lines to a new error log file.
Conclusion
Reading a file line by line in PowerShell can be achieved using several methods:
- Using Get-Content and a Foreach Loop
- Streaming File Content
- Using ForEach-Object Cmdlet
- Using [System.IO.File]::ReadLines()
- Using Stream Reader
- Using Switch Statement
Whether you’re processing small or large files, PowerShell provides you with different ways to read a file line by line in PowerShell.
You may also like:
- How to Replace Text in a File Using PowerShell?
- How to Download File from URL in PowerShell?
- How To Delete File If Exists In PowerShell?
- How to Copy Files from One Folder to Another in PowerShell?
- How to Write Variables to a File in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.