Search code examples
powershellautomation

Can you read from multiple columns using a single foreach loop?


I'm creating a script to read from a imported csv file. There are two columns titled 'userID' and 'domain' respectively, which are used to create an email address based on the domain listed ("texas", "orlando",etc).

I currently have this to iterate through the IDs manually placed, but would also like to grab the values from the second column and work with them.

forEach ($E in $ws.'userID') {

"do something"

}

Have this to start with but I don't think it will work how I want it to

forEach ($E in $ws.'userID', $D in $ws.'domain') {

"do something"

}

Solution

  • Sure, assuming $ws is an array or list of objects that each have userID and domain properties:

    foreach($e in $ws){
      # do something with $e.userID
      # do something with $e.domain
    }