SOLVED

Migrating from Get-AzureADUser to Get-MgUser

Copper Contributor

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.

4 Replies
For generic guidance, you can try this tool: https://graphpowershell.merill.net/

For the specific example, use Filter:

https://graph.microsoft.com/beta/users?$filter=(companyName ne null and (companyName eq 'ZZZ' or companyName eq 'YYY'))

Also, when using advanced queries/ConsistencyLevel, you must use $count=true (-CountVariable blabla) as well.

@Vasil Michev 

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?

best response confirmed by Melbatoast (Copper Contributor)
Solution
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.
Thank you! Adding the -CountVariable option worked. As for the other part of my question (which wound up weird because I think this site removed my fake email address and replaced it with a message), I'm not sure what happened a few days ago. Both commands now match.
1 best response

Accepted Solutions
best response confirmed by Melbatoast (Copper Contributor)
Solution
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.

View solution in original post