Get-MgUser - Invalid filter clause

1 minute read

I recently started to dig into the Microsoft Graph PowerShell module initially to do some Azure AD stuff, but ultimately to unlock the full potential of the Graph API using PowerShell 7 (PowerShell Core). I recently started a new job and I’m trying my darndest to be PowerShell 7 all the time. At this point it is impossible because I have to support Exchange Online as well, but hopefully that will someday be PowerShell 7 compatible.

Get-MgUser

The first thing I wanted to replace was Get-AzureADUser, the natural replacement in the Graph module is Get-MgUser. Like the AzureAD module, you can’t call users directly by their UPN or any other easily remembered property, you have to either know their ID or use a search or filter. Personally I prefer the filter so I can match specifically on a UPN (or other property as needed).

So, in the AzureAD module, filtering for a UPN looks like:

Get-AzureADUser -Filter "UserPrincipalName eq 'anthony@howell-it.com'"

Very straight forward! But, if you are used to filtering on Get-ADUser you’ll likely think that it looks wrong because there is no - in front of the comparison operator. Yeah, me too, but that is how it is for Azure AD through either the AzureAD module or through the Graph API module. So you need the same syntax for Get-MgUser:

Get-MgUser -Filter "UserPrincipalName eq 'anthony@howell-it.com'"

If you use double quotes in the string or prepend the comparison operator with a - (like I tried initially), you will end up with a red error:

Get-MgUser_List: Invalid filter clause

This is a really simple solution that threw me for enough time that I figured I’d write up a quick blog post since I didn’t see any other specifically for Get-MgUser.

Leave a Comment