Home > Mobile >  how can I get groups of some user, then filter groups by some name?
how can I get groups of some user, then filter groups by some name?

Time:12-08

I wanto to get all groups some user belongs to, then filter groups by some name, then display property "description" for these groups.

I get groups of user using this command

$mem = ( Get-AdUser someLogin -Properties MemberOf ).MemberOf

Now I can get description for all these groups using this command - this works fine:

$mem | Get-AdGroup -Properties Description | Select-Object Name, Description

name        description
----        ------------
group100    descr. 100
group222    descr 222
....
group999    descr. 999

What I want is to display only some groups that contains some string, let say "888".
I try the below command, but there is an error that I don't understand:

$mem | Select-String '888' | Get-AdGroup -Properties Description | Select-Object Name, Description

Get-AdGroup : The input object cannot be bound to any parameters for the command either because the command 
does not take pipeline input or the input and its properties do not match any of the parameters 
that take pipepline input.

CodePudding user response:

This should work

$mem | Select-String '888' | Select-Object -ExpandProperty Line | Get-ADGroup -Properties Description | Select-Object Name, Description

The output from your Select-String is an array of Microsoft.PowerShell.Commands.MatchInfo-objects so in order for you to be able to actually get the AD-groups you need expand the property with the line that was actually matched (Line). You could run ($mem | select-string '888')[0] | select * to inspect the object further.

Another, perhaps more common way, would be to utilize Where-Object - for instance something like

$mem | Where-Object { $_ -like "*888*" } | Get-ADGroup -Properties Description | Select-Object Name, Description
  • Related