Do you want to read file line by line in PowerShell? In this PowerShell tutorial, I will explain how to read a file line by line into an array in PowerShell using various methods.
To read a file line by line into an array in PowerShell, use the Get-Content cmdlet which automatically returns an array of strings, with each line from the file as an element in the array. For example, $array = Get-Content -Path ‘filename.txt’ reads ‘filename.txt’ and stores each line as an element in $array.
Read File Line By Line Into Array In PowerShell
There are multiple methods to read files line by line into an array in PowerShell. Let us check out each method with examples:
1. Using Get-Content
In PowerShell, the simplest way to read a file line by line into an array is by using the Get-Content cmdlet. This cmdlet reads each line of a file and creates an array where each line is an element of the array.
Here is a complete example.
# Read file into an array
$lines = Get-Content -Path "C:\Bijay\fruits.txt"
# Display the array
$linesIn this example, Get-Content reads “fruits.txt” and stores each line as an element in the $lines array. You can then manipulate or access the lines using their index in the array.
You can see the output in the screenshot below; I executed the script using VS code.

2. Using Import-Csv
If the file you’re reading is structured as a CSV (Comma-Separated Values), you can use the Import-Csv cmdlet. This cmdlet reads the CSV file and creates an array of custom objects, where each object represents a line in the CSV file.
Here is an example.
# Read CSV file into an array of objects
$rows = Import-Csv -Path "C:\path\to\your\file.csv"
# Display the array
$rowsEach object in the $rows array will have properties corresponding to the column headers in the CSV file.
Method 3: Using StreamReader
For very large files, Get-Content might not be the most efficient method because it reads the entire file into memory. In such cases, you can use the StreamReader class from the System.IO namespace. This allows you to read the file line by line without loading the entire file into memory.
Here is an example of reading a file line by line into an array in PowerShell using StreamReader.
# Create a StreamReader object
$reader = [System.IO.StreamReader] "C:\path\to\your\file.txt"
# Create an array to hold the lines
$lines = @()
# Read each line from the file and add it to the array
while ($line = $reader.ReadLine()) {
$lines += $line
}
# Close the StreamReader
$reader.Close()
# Display the array
$linesThis method is more memory-efficient and is better suited for large files.
Method 4: Using ForEach-Object
Another way to read a file into an array in PowerShell is by using the ForEach-Object cmdlet in combination with Get-Content. This method allows you to process each line as it’s being read, which can be useful for filtering or modifying the data on-the-fly.
Here is an example:
# Read file and use ForEach-Object to process each line
$lines = Get-Content -Path "C:\path\to\your\file.txt" | ForEach-Object {
# Process each line here if needed
$_
}
# Display the array
$linesIn this example, $_ represents the current line being processed. You can add any processing code inside the script block {}.
Conclusion
PowerShell provides multiple ways to read a file line by line into an array, each with its own use cases and advantages. For small to medium-sized files, Get-Content is the simplest and most straightforward method. For CSV files, Import-Csv is a powerful cmdlet that creates an array of objects with properties. For large files, consider using StreamReader to avoid high memory usage. And finally, ForEach-Object can be used when you need to process lines as you read them.
In this tutorial, I have explained how to read file line by line into array in PowerShell using different methods with examples.
You may also like:
- How to Export an Array to CSV in PowerShell?
- How to Read JSON File into Array in PowerShell?
- How To Write Array To 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.