Read file line by line in PowerShell

powershell read file line by line into array
powershell read text file into variable
powershell write to file
powershell streamreader
powershell get-content foreach
powershell read first line of file
powershell open text file in notepad++
powershell find string in text file

I want to read a file line by line in PowerShell. Specifically, I want to loop through the file, store each line in a variable in the loop, and do some processing on the line.

I know the Bash equivalent:

while read line do
    if [[ $line =~ $regex ]]; then
          # work here
    fi
done < file.txt

Not much documentation on PowerShell loops.

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
}

How to read a text file in PowerShell quickly and easily, Reading line-by-line. Using Get-Content. The Get-Content function reads every line in the text and stores them as an array, where each line is an array element. Using StreamReader class. Count the number of lines in the file. Specific number of lines at the beginning and the end. Continuously updated file. PowerShell ISE’s output window only returns the last five lines of the file. PowerShell’s built-in Get-Content function can be useful, but if we want to store very little data on each read for reasons of parsing, or if we want to read line by line for parsing a file, we may want to use .NET’s StreamReader class, which will allow us to customize our usage for increased efficiency.

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).

Get-Content (Microsoft.PowerShell.Management), We may face a situation where we want to read every line except the first In the below script, we output an entire file's data on the PowerShell� I want to read a file line by line in PowerShell. Specifically, I want to loop through the file, store each line in a variable in the loop, and do some processing on the line. I know the Bash equi

The almighty switch works well here:

'one
two
three' > file

$regex = '^t'

switch -regex -file file { 
  $regex { "line is $_" } 
}

Output:

line is two
line is three

Reading file data with PowerShell, You can use PowerShell Get-Content to list the contents of a file. Then use PowerShell ForEach to loop throuh the file line by line. See How. The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function. For files, the content is read one line at a time and returns a collection of objects, each of which represents a line of content. Beginning in PowerShell 3.0, Get-Content can also get a specified number of lines from the beginning or end of an item.

Foreach in File Powershell: How to Iterate the Content of a Text File, In this text file I have the three line shown below and I'd like to get the contents of MyText.txt with a PowerShell script. To do this, I can use� Then use PowerShell ForEach to loop through the file line by line. An example could be a list with Active Directory user names in a text file. You can use the usernames from the text file to search for the users in AD, then update other user information in AD.

How to read text in text files with PowerShell, PowerShell has a Get-Content cmdlet that reads a text file into the buffer one line at a time. This feature is especially handy for use in loops to extract the same information from several systems within a single script. Consider the following comparatively simple Linux Bash script that reads in a text file (systems. There's no need to pick through the file line-by-line; PowerShell can do this for you using select-string. The -notMatch parameter simply inverts the search and sends through any lines that don't match the pattern.

Keeping PowerShell in the Loop � ADMIN Magazine, Solution: Exactly as tonycortes said, either edit your text file so each line looks like that:T4TdfEnc.cfg . File1 T4TdfEnc.cfg . File2 T4TdfEnc.cfg . I have one hosts file which is having multiple host names and IP addresses. Below is the example of the input file. 127.0.0.1 Host1 Host2 Host3 127.0.0.2 Host4 Host5 Host6 I want to read each line and ping for the first host (Host1), then the second host (Host2) and then the third host (Host3).

Comments
  • The selected answer from Mathias is not a great solution. Get-Content loads the entire file into memory at once, which will fail or freeze on large files.
  • @KolobCanyon that is completely untrue. By default Get-Content loads each line as one object in the pipeline. If you're piping to a function that doesn't specify a process block, and spits out another object per line into the pipeline, then that function is the problem. Any problems with loading the full content into memory are not the fault of Get-Content.
  • @TheFish foreach($line in Get-Content .\file.txt) It will load the entire file into memory before it begins iterating. If you don't believe me, go get a 1GB log file and try it.
  • @KolobCanyon That's not what you said. You said that Get-Content loads it all into memory which is not true. Your changed example of foreach would, yes; foreach is not pipeline aware. Get-Content .\file.txt | ForEach-Object -Process {} is pipeline aware, and will not load the entire file into memory. By default Get-Content will pass one line at a time through the pipeline.
  • The links are not broken, but they now redirect to docs.microsoft.com.
  • @KolobCanyon that was never mentioned as an issue on the OP.
  • I would probably use [System.IO.File]::ReadLines("C:\path\to\file.txt") | ForEach-Object { ... }. The foreach statement will load the entire collection to an object. ForEach-Object uses a pipeline to stream with. Now the foreach statement will likely be slightly faster than the ForEach-Object command, but that's because loading the whole thing to memory usually is faster. Get-Content is still terrible, however.
  • @BaconBits foreach() is an alias of Foreach-Object
  • That is a very common misconception. foreach is a statement, like if, for, or while. ForEach-Object is a command, like Get-ChildItem. There is also a default alias of foreach for ForEach-Object, but it is only used when there is a pipeline. See the long explanation in Get-Help about_Foreach, or click the link in my previous comment which goes to an entire article by Microsoft's The Scripting Guys about the differences between the statement and the command.
  • @BaconBits blogs.technet.microsoft.com/heyscriptingguy/2014/07/08/… Learned something new. Thanks. I assumed they were the same because Get-Alias foreach => Foreach-Object, but you are right, there are differences
  • That will work, but you'll want to change $line to $_ in the loop's script block.