The New-MgGroupOwnerByRef cmdlet is a powerful tool in the Microsoft Graph PowerShell module that allows administrators to add one or more owners to a Microsoft 365 group. This cmdlet is particularly useful for delegating management responsibilities within an organization, ensuring that group management tasks can be shared among multiple users.
Before using the New-MgGroupOwnerByRef cmdlet, ensure you have the following:
Install-Module Microsoft.Graph
Group.ReadWrite.All
and Directory.ReadWrite.All
permissions.Connect-MgGraph -Scopes "Group.ReadWrite.All" "Directory.ReadWrite.All"
New-MgGroupOwnerByRef -GroupId <String> -BodyParameter <Hashtable>
Parameters:
-GroupId:
The unique identifier of the group to which you want to add the owner.-BodyParameter:
A hashtable that specifies the @odata.id
of the user you want to add as an owner.$groupId = "your-group-id"
$ownerId = "new-owner-id"
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$ownerId"
}
New-MgGroupOwnerByRef -GroupId $groupId -BodyParameter $body
$groupId = "your-group-id"
$ownerIds = @("owner-id-1", "owner-id-2", "owner-id-3")
foreach ($ownerId in $ownerIds) {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$ownerId"
}
New-MgGroupOwnerByRef -GroupId $groupId -BodyParameter $body
}
Solution: Ensure that your account has the necessary permissions (Group.ReadWrite.All
and Directory.ReadWrite.All
).
Solution: Verify that the GroupId
and DirectoryObjectId
are correct and that the resources exist in your tenant.
Solution: Use a try-catch block to handle this error gracefully. You can skip adding the owner if they already exist.
try {
$body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$ownerId"
}
New-MgGroupOwnerByRef -GroupId $groupId -BodyParameter $body
} catch {
if ($_.Exception.Message -like "*already an owner*") {
Write-Host "User $ownerId is already an owner of the group $groupId."
} else {
throw $_
}
}
The New-MgGroupOwnerByRef cmdlet is an essential tool for Microsoft 365 administrators, enabling efficient management of group ownership. By understanding the prerequisites, syntax, and usage examples, you can leverage this cmdlet to streamline group management tasks. Always ensure to handle possible errors and implement best practices for a smooth administrative experience.
For further information, refer to the official Microsoft documentation.
© m365corner.com. All Rights Reserved. Design by HTML Codex