Forum Discussion

Melbatoast's avatar
Melbatoast
Copper Contributor
May 08, 2024
Solved

Migrating from Get-AzureADUser to Get-MgUser

I am attempting to replicate a Get-AzureADUser command using Get-MgUser. The previous command (Get-AzureADUser -Top 5000 | Where-Object {($.AccountEnabled -eq "True") -and (($.CompanyName -eq "CompA" -or $.CompanyName -eq "CompB" -or $.CompanyName -eq "CompC" -or $_.CompanyName -eq "CompD"))} | Select UserPrincipalName) selected the desired accounts. I have gotten this far with the replacement: Get-MgUser -All -Filter "AccountEnabled eq true" -Search "CompanyName:CompA" -ConsistencyLevel: eventual | Select UserPrincipalName

I have been unsuccessful in adding more search options for the CompanyName field. How do I use the -Search option to search for multiple values?

I noticed another discrepancy between the output of the two commands. Why does 'Get-AzureADUser -ObjectID email address removed for privacy reasons' display data for MailNickname but 'Get-MgUser -UserID email address removed for privacy reasons' shows the field as blank?

Since Microsoft is forcing us to rewrite all our scripts, it would be useful to have some assistance.

  • The example above works just fine in PowerShell, too:

    Get-MgUser -Filter "companyName ne null and (companyName eq 'ZZZ' or companyName eq 'YYY')" -ConsistencyLevel eventual -CountVariable count

    Again, use consistencyLevel/count.

4 Replies

    • Melbatoast's avatar
      Melbatoast
      Copper Contributor

      VasilMichev 

      Thank you for the suggestion.  The reason I resorted to the -Search option for the CompanyName field is I cannot get -Filter to recognize CompanyName.  If I dumb the query down to:

      Get-MgUser -All -Filter "DisplayName eq 'First Last'"

      it works.  If I replace the filter with this:

      Get-MgUser -All -Filter "CompanyName eq 'CompA'"

      I get this error:

      Unsupported or invalid query filter clause specified for property 'companyName'

      It seems Company Name can only be used with the -Search option.  Thoughts?

      • VasilMichev's avatar
        VasilMichev
        MVP
        The example above works just fine in PowerShell, too:

        Get-MgUser -Filter "companyName ne null and (companyName eq 'ZZZ' or companyName eq 'YYY')" -ConsistencyLevel eventual -CountVariable count

        Again, use consistencyLevel/count.

Resources