Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitNote: Use Get-MgDirectoryAdministrativeUnit to fetch Administrative Unit (AU) IDs before adding members.
This cmdlet adds directory objects—users, groups, or devices—to an Administrative Unit (AU) in Microsoft Entra ID using a reference payload (@odata.id). It maps directly to the Graph API’s .../administrativeUnits/{id}/members/$ref endpoint.
New-MgDirectoryAdministrativeUnitMemberByRef
-AdministrativeUnitId
-BodyParameter # must include "@odata.id"
Required Graph permissions (one of):
Success result: returns nothing (HTTP 204 NoContent under the hood).
All examples strictly follow the -BodyParameter hashtable pattern expected by the Graph PowerShell SDK. Replace placeholder IDs with real ones from your tenant.
# Inputs
$administrativeUnitId = "00000000-0000-0000-0000-000000000000" # AU Id (use Get-MgDirectoryAdministrativeUnit to fetch ID)
$userId = "11111111-1111-1111-1111-111111111111" # User Id (use Get-MgUser)
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$userId"
}
New-MgDirectoryAdministrativeUnitMemberByRef `
-AdministrativeUnitId $administrativeUnitId `
-BodyParameter $params
$administrativeUnitId = "00000000-0000-0000-0000-000000000000"
$upns = @(
"alexw@contoso.com",
"meganb@contoso.com",
"diegoS@contoso.com"
)
foreach ($upn in $upns) {
$id = (Get-MgUser -UserId $upn).Id
$params = @{ "@odata.id" = "https://graph.microsoft.com/v1.0/users/$id" }
New-MgDirectoryAdministrativeUnitMemberByRef -AdministrativeUnitId $administrativeUnitId -BodyParameter $params
}
CSV layout (save as upns-to-au.csv):
UserPrincipalName
alexw@contoso.com
meganb@contoso.com
diegoS@contoso.com
Script:
# Bulk add users (by UPN) to an Administrative Unit
# Prereqs: Connect-MgGraph; ensure you have AdministrativeUnit.ReadWrite.All
# Tip: Use Get-MgDirectoryAdministrativeUnit to fetch the AU Id.
$administrativeUnitId = "00000000-0000-0000-0000-000000000000" # replace
$csvPath = ".\upns-to-au.csv"
$rows = Import-Csv -Path $csvPath
foreach ($row in $rows) {
$upn = ($row.UserPrincipalName).Trim()
if (-not $upn) {
Write-Warning "Row has an empty 'UserPrincipalName'. Skipping."
continue
}
try {
# Resolve UPN to Id
$user = Get-MgUser -UserId $upn -ErrorAction Stop
# Build reference payload
$params = @{ "@odata.id" = "https://graph.microsoft.com/v1.0/users/$($user.Id)" }
# Add to AU
New-MgDirectoryAdministrativeUnitMemberByRef `
-AdministrativeUnitId $administrativeUnitId `
-BodyParameter $params `
-ErrorAction Stop
Write-Host ("Added {0} ({1}) to AU {2}" -f $upn, $user.Id, $administrativeUnitId)
}
catch {
Write-Warning ("Failed to add {0}: {1}" -f $upn, $_.Exception.Message)
}
}
$administrativeUnitId = "00000000-0000-0000-0000-000000000000"
$groupId = (Get-MgGroup -Filter "displayName eq 'HR Managers'").Id
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/groups/$groupId"
}
New-MgDirectoryAdministrativeUnitMemberByRef -AdministrativeUnitId $administrativeUnitId -BodyParameter $params
$administrativeUnitId = "00000000-0000-0000-0000-000000000000"
$deviceId = (Get-MgDevice -Filter "displayName eq 'LAPTOP-42'").Id
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/devices/$deviceId"
}
New-MgDirectoryAdministrativeUnitMemberByRef -AdministrativeUnitId $administrativeUnitId -BodyParameter $params
The value must be a fully qualified Graph v1.0 URL:
Error | Cause | Solution |
---|---|---|
Authorization_RequestDenied / 403 | Missing permission | Grant AdministrativeUnit.ReadWrite.All. Re-authenticate if needed. |
ResourceNotFound (404) | Wrong AU ID or object not found | Confirm AU ID with Get-MgDirectoryAdministrativeUnit and user/group/device IDs with Get-MgUser, Get-MgGroup, or Get-MgDevice. |
Request_BadRequest (400) | Malformed @odata.id | Ensure it follows the correct format: https://graph.microsoft.com/v1.0/{entity}/{id}. |
Request_ResourceAlreadyExists (409) | Member already added | Handle gracefully in loops, or check membership before adding. |
Throttling (429) | Too many requests | Add delays (Start-Sleep) or implement retry logic. |
New-MgDirectoryAdministrativeUnitMemberByRef is the Graph-native way to add users, groups, and devices to Administrative Units. With support for single adds, arrays, and CSV imports, it’s well suited for both small and large-scale admin scenarios.
Stick to the -BodyParameter with @odata.id, confirm AU IDs with Get-MgDirectoryAdministrativeUnit, and ensure proper permissions. With these best practices, you’ll have a reliable, automatable method for scoped administration at scale.
© m365corner.com. All Rights Reserved. Design by HTML Codex