Search code examples
powershellazure-active-directory

Searching for multiple strings in Azure AD groups


We have Azure AD groups with the following naming convention: Department_GroupName_Membership type (with three options: visitor/member/owner). I need to be able to add users to all groups from Department_XY with the membership type "Member". I.e. something like Department_XY_{GroupName}_Member.

I cannot figure out how to retrieve a list of such groups. ODATA filter clauses that should be taken by -Filterparameter of Get_AzureAdGroupCmdlet work strangely.

For example Get-AzureADGroup -Filter "startswith(DisplayName, 'Department_XY')" works fine, however, Get-AzureADGroup -Filter "endswith(DisplayName, 'Member') throws an error.

So I assume there is some better way how to do that?

I have AzureAD module installed.

.....


Solution

  • -SearchString appears to not accept any wildcards and only searches the beginning of the DisplayName values, i.e. an effective .StartsWith(string).

    -Filter uses the OData v3 query language (unless it has been updated to v4). However, for whatever reason, many functions are not available including endswith() and substringof().

    You can use a combination of -SearchString and Where-Object or -Filter and Where-Object to create an efficient query.

    Get-AzureAdGroup -SearchString 'Department_XY' -All $true |
        Where-Object { $_.DisplayName.EndsWith('Member') }