Charlypop
Charlypop

Reputation: 176

Get AD users out of mutliple Groups

I need to export a list that shows all users member of three different groups.

Here is my first try:

Import-Module ActiveDirectory

$desktop = Get-ADGroupMember -Identity "group1" | Select-Object -ExpandProperty samaccountname
$officetd = Get-ADGroupMember -Identity "group2" | Select-Object -ExpandProperty samaccountname
$officepro = Get-ADGroupMember -Identity "group3" | Select-Object -ExpandProperty samaccountname

I have tried to filter out by piping the first variable like this :

$desktop | Where-Object {$_ -contains $officetd}

But it won't work.

Any idea how I can do that?

Upvotes: 0

Views: 55

Answers (1)

Olaf
Olaf

Reputation: 5232

Almost .... try this:

$desktop | Where-Object {$_ -in $officetd -and $_ -in $officepro}

For this task you have a few slightly different options. Either you check if a single element is in a collection of elements, like the code above does it. Or you check if a collection of elements contains a single element, like the code below does it:

$desktop | Where-Object {$officetd -contains $_ -and $officepro -contains $_}

So it is important to chose the right "direction" of the comparison.

Upvotes: 1

Related Questions