branedge
branedge

Reputation: 135

powershell 2 working with multiple csv files

I have 2 csv files. One with names and the other with a list of phone numbers. i'm trying to loop through each csv file and run a script on the user and name.

file examples
Name Number
a      1
b      2
c      3

So I need to run a script like a + 1, b + 2, c + 3

trying to use a foreach loop but it not working correctly it's nested incorrectly and can't figure it out.

 if ($users){
foreach ($u in $users) 
{ 
 $username= ($u.'Mail')

  
     foreach ($n in $numbers)
     { 
    
    $number = ($n.'ID')
   
     }  

Upvotes: 1

Views: 317

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36297

You're close, in that you need to use a loop, but you want one loop to get data from both files, so in this case you're best off using a For loop like this:

$Combined = For($i = 0; $i -lt $users.count; $i++) {
    $Props = @{
        Mail = $users[$i].Mail
        ID = $numbers[$i].ID
    }
    New-Object PSObject -Prop $Props
}
$Combined | Export-Csv C:\Path\To\Combined.csv -NoTypeInfo

Upvotes: 2

Related Questions