0

I have an array with the following values:

| Firstname | Lastname         | Username |
|-----------|------------------|----------|
| person1   | person1_lastname | p1       |
| person2   | person2_lastname | p2       |
| person3   | person3_lastname | p3       |
| person4   | person4_lastname | p4       |

This is the code that produces the above results:

$finalUsers = foreach($person in $excludedUsers) {
    if ($person.Username -notin $ausLunchJobs.AssigneeUser -and $person.Username -notin $ausLunchJobs.AssigneeUser2) {
        $person | Select-Object Firstname, Lastname, Username
    }
}

I want to split that array into two columns and pair the Username data together.

Ideal output:

| Username | Username2 |
|----------|-----------|
| p1       | p2        |
| p3       | p4        |

Any guidance on how I can achieve something like this?

TIA

0

1 Answer 1

1

Create 1 new object per row you want displayed in the table:

$userPairs = for($i = 0; $i -lt $finalUsers.Count; $i += 2){
  $finalUsers[$i] |Select-Object Username,@{ Name='Username2'; Expression={ $finalUsers[$i+1].username } }
} 

Result:

PS ~> $userPairs |Format-Table 

Username Username2
-------- ---------
p1       p2
p3       p4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.