2

I'm trying to split up an array of user objects retrieved via $users = Get-QADUser -searchroot 'domain.net/OU1/OU2/Users' into two arrays based on their 'AccountIsExpired'-property.

I can do $users = $users | where {!($_.AccountIsExpired)} but that only gives me one part of my users. However, this does filter out some user objects.

I build this code to split $users into two arrays ($include, $exclude):

[Object[]]$include = @();
[Object[]]$exclude = @();
foreach ($user in $users) {
    if (!($_.AccountIsExpired)) {
        $include += $user;
    } else {
        $exclude += $user;
    }
}
$users = $include;

Apart from the creation of a second array, I don't see the difference between those two bits of code...

2
  • Okay, looking at your code in a browser window sometimes is all you need to resolve your issue -.- Here's the code that works: [Object[]]$include = @(); [Object[]]$exclude = @(); foreach ($user in $users) { if (!($user.AccountIsExpired)) { # copied $_ from the where filter before... $include += $user; } else { $exclude += $user; } } $users = $include; $exclude; Commented Nov 15, 2011 at 20:47
  • sorry for being unclear, thanks anyways! Commented Nov 15, 2011 at 20:48

1 Answer 1

7

What about using group-object?

Something like $groups=$users | group-object AccountIsExpired

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That looks very good, just tried it on the command line and should work in my script, too... Will try it later.

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.