Search code examples
powershellforeachenumeration

foreach with mutliple variables foreach($number) in Powershell


I want to use multiple inputs in foreach like this:

foreach($numberin in [System.IO.File]::ReadLines(".\numberin.txt") & ($numberout in [System.IO.File]::ReadLines(".\numberout.txt")) 
{ ... }

But somehow "&" isn't working there.

like this

foreach($numberin in [System.IO.File]::ReadLines(".\numberin.txt") -and ($numberout in [System.IO.File]::ReadLines(".\numberout.txt"))

it tells me "unexpected token"

like this

foreach($numberin in [System.IO.File]::ReadLines(".\numberin.txt") -and ($numberout in [System.IO.File]::ReadLines(".\numberout.txt")

it tells me I need a closing ")" for the foreach

To clarify what I want to do: I want to load some input from two files in two variables and do something with them each time it loads a new line from each file.


Solution

  • That's what I wanted to do:

    #Begin with first line in "numberout.txt"
    $counter = 0
    
    foreach ($numberin in [System.IO.File]::ReadLines(".\numberin.txt")){
    $numberout = (Get-Content file.txt)[$counter]
    $counter = $counter +1
    

    maybe someone can tell me how to replace my "Get-Content" with [System.IO.File] too.