victorsionado
victorsionado

Reputation: 99

Get-AzureADGroup for groups starting with XX and YY

I need to know how can I get a list of all groups starting with XX and YY.

So far I've tried

Get-AzureADGroup -SearchString 'XX'
Get-AzureADGroup -SearchString 'YY'
Get-AzureADGroup -SearchString 'ZZ'

It shows the result, but I was wondering how to do it in one query.

I've also tried

$group = 'XX', 'YY', 'ZZ'
foreach($groupNames in $group) {
Get-AzureADGroup}

But it shows things it shouldn't and gets every result 3 times.

Upvotes: 1

Views: 2063

Answers (2)

AdminOfThings
AdminOfThings

Reputation: 25001

The -SearchString parameter applies an implicit StartsWith functionality. So -SearchString 'XX' would match XX My Group but not MY Group XX. Therefore, you can do the following:

$group = 'XX', 'YY', 'ZZ'
foreach($groupName in $group) {
    Get-AzureADGroup -SearchString $groupName
}

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

Get-AzureADGroup supports OData filters, so you should be able to do a wildcard search for groups starting with XX by doing:

Get-AzureADGroup -Filter "startswith(Name, 'XX')"

With that in mind, we can generate a filter statement like so:

# define prefixes
$group = 'XX', 'YY', 'ZZ'

# generate filter clauses and concatenate
$filter = $group.ForEach({"startswith(Name,'${_}')"}) -join ' or '

# retrieve matching groups
Get-AzureADGroup -Filter $filter

Upvotes: 1

Related Questions