In Microsoft Graph API, and by extension the PowerShell module, entities or resources like users, groups, or messages have relationships with other entities. For example, a user might have manager details or a group could have members. These related resources can be expanded inline with the -Expand parameter.
Note: Without -Expand parameter, you will be required to make separate Graph API calls to fetch such nested resources.
Here are some practical examples of how to use the -Expand parameter in Microsoft Graph PowerShell module commands:
Here's the Graph PowerShell script that fetches the manager details of a user with the help of -Expand property.
# Retrieve the user along with their manager's details
#Use the user id of the user whose manager you wish to fetch
$userWithManager = Get-MgUser -UserId "9ccc0d2b-ff1f-4d13-9dcf-a42749fd27ba" -Expand "manager"
# Output the manager's details
if ($userWithManager.Manager -ne $null) {
# Extracting properties from AdditionalProperties dictionary
$managerName = $userWithManager.Manager.AdditionalProperties["displayName"]
$managerEmail = $userWithManager.Manager.AdditionalProperties["mail"]
$managerJobTitle = $userWithManager.Manager.AdditionalProperties["jobTitle"]
# Check each property for null value and display appropriate message if null or empty
$managerName = if ([string]::IsNullOrWhiteSpace($managerName)) { "Not Available" } else { $managerName }
$managerEmail = if ([string]::IsNullOrWhiteSpace($managerEmail)) { "Not Available" } else { $managerEmail }
$managerJobTitle = if ([string]::IsNullOrWhiteSpace($managerJobTitle)) { "Not Available" } else { $managerJobTitle }
Write-Output "Manager's Details:"
Write-Output "Name: $managerName"
Write-Output "Email: $managerEmail"
Write-Output "Job Title: $managerJobTitle"
} else {
Write-Output "This user does not have a manager assigned."
}
This script efficiently retrieves and displays both the user and their manager's details using a single call to the Microsoft Graph API, thanks to the -Expand parameter. This parameter minimizes the need for additional API calls and simplifies data management by pulling related information in one go.
Here's the Graph PowerShell script that fetches the direct reports to a manager with the help of -Expand property
# Retrieve the user along with their direct reports
$userWithDirectReports = Get-MgUser -UserId "1b3ed1a5-438e-4ce9-9f63-f880991afd3a" -Expand "directReports"
# Output the direct reports' details
if ($userWithDirectReports.DirectReports -ne $null) {
Write-Output "Direct Reports:"
foreach ($report in $userWithDirectReports.DirectReports) {
# Extracting properties from AdditionalProperties dictionary
$name = $report.AdditionalProperties["displayName"]
$email = $report.AdditionalProperties["mail"]
$jobTitle = $report.AdditionalProperties["jobTitle"]
# Check each property for null value and display appropriate message if null or empty
$name = if ([string]::IsNullOrWhiteSpace($name)) { "Not Available" } else { $name }
$email = if ([string]::IsNullOrWhiteSpace($email)) { "Not Available" } else { $email }
$jobTitle = if ([string]::IsNullOrWhiteSpace($jobTitle)) { "Not Available" } else { $jobTitle }
Write-Output "Name: $name"
Write-Output "Email: $email"
Write-Output "Job Title: $jobTitle"
}
} else {
Write-Output "This user has no direct reports."
}
Here's the Graph PowerShell script that fetches the Group IDs to which the user belongs to with the help of -Expand property
# Retrieve the user and expand the memberOf property
$userWithGroups = Get-MgUser -UserId "9ccc0d2b-ff1f-4d13-9dcf-a42749fd27ba" -Expand "memberOf"
# Output the groups the user is a member of
if ($userWithGroups.MemberOf -ne $null) {
Write-Output "Member of the following groups:"
foreach ($group in $userWithGroups.MemberOf) {
Write-Output "Group ID: $($group.Id)"
}
} else {
Write-Output "This user is not a member of any groups."
}
What does the -ExpandProperty parameter do in Graph PowerShell?
The -ExpandProperty parameter retrieves related or nested properties that are not returned by default. It is mainly used when fetching related objects, such as a user's manager, direct reports, or group members.
How is -ExpandProperty different from -Property?
-ExpandProperty fetches related objects in a single request (e.g., fetching a user’s manager along with the user). -Property retrieves additional fields but does not expand related objects.
Can I expand multiple properties at once?
Yes, you can expand multiple properties by separating them with commas.
Get-MgUser -UserId user@example.com -ExpandProperty Manager, DirectReports
How do I get group members using -ExpandProperty?
Use -ExpandProperty Members to get all members of a Microsoft 365 Group:
Get-MgGroup -GroupId -ExpandProperty Members
This fetches group details along with its members.
What’s the best alternative if -ExpandProperty doesn’t work?
If -ExpandProperty isn’t supported, use a separate API call. For example, if DirectReports is not available, run:
Get-MgUserDirectReport -UserId user@example.com
© m365corner.com. All Rights Reserved. Design by HTML Codex