/flags/microsoft-graph/get-mguser-property
Get-MgUser -Property: why properties are empty and how to select them
- tool
- Microsoft Graph PowerShell
- command
- Get-MgUser
- flag
- -Property (alias -Select)
- default
- Graph default set: businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName
Get-MgUser -Property DisplayName,AccountEnabled,Department asks Microsoft Graph for specific user properties. By default Graph returns only a limited set: "By default, only a limited set of properties are returned (businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, and userPrincipalName)." Everything else (AccountEnabled, Department, UsageLocation, CreatedDateTime, SignInActivity…) exists on the returned object but is empty until you request it. -Property maps to the OData $select query option and has the alias -Select.
Connect-MgGraph -Scopes "User.Read.All"
Get-MgUser -UserId anna@contoso.com -Property DisplayName, UserPrincipalName, AccountEnabled, Department, UsageLocation |
Format-List DisplayName, UserPrincipalName, AccountEnabled, Department, UsageLocation
Get-MgUser -All -Property Id, DisplayName, AccountEnabled, CreatedDateTime |
Select-Object DisplayName, AccountEnabled, CreatedDateTimeThe parameter in the module
-Property is a string array available in every parameter set of Get-MgUser, with the alias Select. Checked offline in Windows PowerShell 5.1 with Microsoft.Graph.Users 2.41.2:
Import-Module Microsoft.Graph.Users
$p = (Get-Command Get-MgUser).Parameters['Property']
"Type: " + $p.ParameterType.Name
"Aliases: " + ($p.Aliases -join ', ')
"Sets: " + (($p.ParameterSets.Keys | Sort-Object) -join ', ')
"ExpandProperty type: " + (Get-Command Get-MgUser).Parameters['ExpandProperty'].ParameterType.NameOutput
Type: String[] Aliases: Select Sets: __AllParameterSets ExpandProperty type: String[]
Properties that need -Property
The user resource marks many properties "Requires $select to retrieve" (Graph $select, i.e. -Property in PowerShell):
| Property | Notes |
|---|---|
| AccountEnabled | sign-in allowed or blocked |
| UsageLocation | needed before assigning licenses |
| PasswordPolicies | DisablePasswordExpiration, DisableStrongPassword |
| CreatedDateTime, Department, CompanyName, EmployeeId | directory attributes for reports |
| OnPremisesSyncEnabled, OnPremisesImmutableId | hybrid (synced) accounts |
| SignInActivity | last sign-in; needs AuditLog.Read.All, and limits the page size to 500 |
| Manager | a relationship: use -ExpandProperty manager, not -Property |
Pitfalls
- Default set is replaced. Once you pass
-Property, list every property you want, includingDisplayNameandUserPrincipalName. - Case does not matter, spelling does. Property names follow the Graph user resource; a misspelled name makes the request fail with an error from the service.
- Relationships. Manager, memberOf and similar are relationships, read with
-ExpandPropertyor separate cmdlets. - SignInActivity. Last sign-in data needs extra permissions and licensing; see last sign-in date.
- All pages.
-Propertydoes not change paging; add-Allfor more than 100 users (Get-MgUser -All).
FAQ
Why is AccountEnabled (or Department, or UsageLocation) empty in Get-MgUser output?
Microsoft Graph returns only a limited default set of user properties. Every other property comes back empty unless you ask for it: Get-MgUser -UserId ID -Property AccountEnabled,Department,UsageLocation.
Does -Property replace the default properties?
Yes. With -Property, Graph returns only the properties you list (plus id), so include DisplayName and UserPrincipalName yourself if you need them.
Why does Select-Object Department still show nothing?
Select-Object only picks from what the cmdlet received. Ask the service for the property with -Property first, then select or format it.