The Get-MgDirectoryObjectById cmdlet in Microsoft Graph PowerShell allows administrators to retrieve directory objects (such as users, groups, and devices) using their unique object IDs. This cmdlet is especially useful for fetching specific directory objects when their identifiers are known.
Get-MgDirectoryObjectById -BodyParameter <Hashtable>
Parameters:
Retrieve Detailed Information for Specific Users
$params = @{
ids = @(
"cc37ba04-e73a-4986-b63e-ce887d726b66"
"d7004a09-ebf0-4e24-bbb9-1d49df68453e"
)
types = @("user")
}
Get-MgDirectoryObjectById -BodyParameter $params | Select-Object Id, DisplayName, UserPrincipalName, AccountEnabled
Explanation: This command retrieves detailed information about the specified users, including their display names, user principal names, and account status.
Retrieve Group Details by Object IDs
$params = @{
ids = @(
"cc37ba04-e73a-4986-b63e-ce887d726b66"
"d7004a09-ebf0-4e24-bbb9-1d49df68453e"
)
types = @("group")
}
Get-MgDirectoryObjectById -BodyParameter $params | Select-Object Id, DisplayName, GroupTypes, SecurityEnabled
Explanation: This command fetches specific details about the requested groups, including their display name, group type, and whether they are security-enabled.
Retrieve Mixed Directory Object Types
$params = @{
ids = @(
"cc37ba04-e73a-4986-b63e-ce887d726b66"
"d7004a09-ebf0-4e24-bbb9-1d49df68453e"
)
types = @(user", "group", "device")
}
Get-MgDirectoryObjectById -BodyParameter $params | Select-Object Id, DisplayName, @{Label='Type';Expression={$_.ODataType}}
Explanation: This example retrieves multiple object types (users, groups, and devices) in a single request and displays their ID, name, and type.
Error | Cause | Solution |
Insufficient privileges to complete the operation. | The signed-in user lacks the required Microsoft Graph permissions | Assign the Directory.Read.All or User.Read.All permission via Connect-MgGraph -Scopes 'Directory.Read.All' |
The provided ID is not valid. | One or more of the provided object IDs are incorrect. | Verify that the object IDs exist and are correctly formatted as GUIDs. |
No results returned. | The specified object type does not match the actual object. | Double-check the types parameter to ensure it corresponds to the correct object category. |
The Get-MgDirectoryObjectById cmdlet is a powerful tool for retrieving directory object details using known object IDs. By leveraging this cmdlet, administrators can efficiently fetch user, group, or device details for auditing, troubleshooting, and identity management tasks. Ensuring the correct object types and permissions are set will help avoid common issues while executing this command.