This guide explains how to use the New-MgGroupOwnerByRef cmdlet in Microsoft Graph PowerShell to assign owners to Microsoft 365 groups. Learn to add single or multiple owners with practical examples for efficient group management.
Managing group ownership in Microsoft 365 is crucial for ensuring proper administration and security. The New-MgGroupOwnerByRef cmdlet allows administrators to add owners to a Microsoft 365 group programmatically. This article will guide you through the usage of this cmdlet, covering syntax examples, cmdlet tips, and a detailed comparison with the New-MgGroupOwner cmdlet.
New-MgGroupOwnerByRef -GroupId <String> -BodyParameter <Hashtable>
Parameters:
-GroupId: The unique identifier (ID) of the group.
-BodyParameter: A hashtable containing the owner information. This parameter follows strict conventions as detailed in the Microsoft documentation.
$Owner = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/{UserId}"
}
New-MgGroupOwnerByRef -GroupId "5c67f5b3-b1c4-4c16-842d-11b453b6f270" -BodyParameter $Owner
Explanation: This command adds a single owner to the specified group. Replace {UserId} with the actual ID of the user you want to add as an owner.
$Owners = @(
@{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/2017e571-90fd-4671-96bb-360c678f4d23"
}
@{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/55bafc67-7ba0-4964-840a-53d480542ab8"
}
)
foreach ($Owner in $Owners) {
New-MgGroupOwnerByRef -GroupId "3a408d7b-d2d1-4ec6-812f-b9ad64187a13" -BodyParameter $Owner
}
Explanation: This example shows how to add multiple owners to a group by looping through the owner data and passing each one to the -BodyParameter.
$OwnersFromCSV = Import-Csv -Path "C:\Path\To\Owners.csv"
foreach ($Owner in $OwnersFromCSV) {
$OwnerHash = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$($Owner.UserId)"
}
New-MgGroupOwnerByRef -GroupId "5c67f5b3-b1c4-4c16-842d-11b453b6f270" -BodyParameter $OwnerHash
}
Explanation: This script imports owner information from a CSV file and adds each owner to the specified group by looping through the imported data and passing each owner’s details to the -BodyParameter.
Checking for the Newly Added Group Owners
Execute Get-MgGroupOwner and provide the Group-Id. The newly added users will get listed.
Cause: The request requires an If-Match header to specify the version of the object being modified.
Solution: Ensure that you are passing the correct headers. If working with the New-MgGroupOwnerByRef cmdlet, ensure that you are following the conventions and not trying to update an existing owner improperly.
Cause: An unsupported query is being used in the request.
Solution: Double-check the parameters and ensure that you are not using unsupported filters or query options. Stick to the basics as provided in the MSFT documentation.
Cause: The URL in the @odata.id field is incorrectly formatted.
Solution: Ensure the @odata.id is correctly pointing to a valid user URL in the format "https://graph.microsoft.com/v1.0/users/{UserId}".
Comparison: New-MgGroupOwnerByRef vs. New-MgGroupOwner
What is New-MgGroupOwnerByRef used for?
New-MgGroupOwnerByRef is a Microsoft Graph PowerShell cmdlet used to assign owners to Microsoft 365 groups by referencing their directory object IDs.
How can I assign a single owner to a group?
You can assign a single owner to a group using the following script:
$Body = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/<OwnerObjectId>"
}
New-MgGroupOwnerByRef -GroupId "<GroupId>" -BodyParameter $Body
Can I assign multiple owners to a group using a CSV file?
Yes, prepare a CSV file with the following format:
GroupId,OwnerReference
<YourGroupId1>,https://graph.microsoft.com/v1.0/directoryObjects/<ObjectId1>
<YourGroupId27gt;,https://graph.microsoft.com/v1.0/directoryObjects/<ObjectId2>
$Data = Import-Csv -Path "C:\Path\To\File.csv"
foreach ($Row in $Data) {
$Body = @{
"@odata.id" = $Row.OwnerReference
}
New-MgGroupOwnerByRef -GroupId $Row.GroupId -BodyParameter $Body
}
What permissions are required to assign group owners?
You need the following permissions to assign group owners:
The New-MgGroupOwnerByRef cmdlet is a powerful tool for managing group ownership in Microsoft 365, especially in complex scenarios requiring bulk additions or CSV imports. While it offers flexibility and power, it requires careful adherence to Microsoft’s documentation to avoid common errors. By understanding its proper usage and potential pitfalls, administrators can effectively leverage this cmdlet to streamline group management processes and ensure compliance with organizational policies.
© m365corner.com. All Rights Reserved. Design by HTML Codex