How to add owners to dynamic Microsoft 365 group with Powershell?

290 Views Asked by At

I can't seem to manage to add 'owners' to a dynamic 365 group with Powershell.

In the GUI it is possible but if fails with Powershell. screen m365 group

https://learn.microsoft.com/en-us/powershell/module/exchange/add-unifiedgrouplinks?view=exchange-ps

First i need to add the users as a member to the group, offcours not working because it is a dynamic group.

PS C:\WINDOWS\system32> Add-UnifiedGroupLinks -Identity "Klas_1A1F_22-23" -LinkType "Members" -Links "a****es.desa*****r@***.be"
Membership for this group is managed automatically by using a rule. Edit the membership rule to change the group membership.
    + CategoryInfo          : NotSpecified: (Klas_1A1F_22-23...5a-2170b2539977:ADObjectId) [Add-UnifiedGroupLinks], DynamicGroupMem...DeniedException
    + FullyQualifiedErrorId : [Server=PA4PR03MB6992,RequestId=cec5e218-c170-4d2d-8108-d529910bc7b5,TimeStamp=13/12/2022 20:28:38] [FailureCategory=Cmdlet-DynamicGroupMembershipChangeDeniedExc
   eption] D3C4100,Microsoft.Exchange.Management.RecipientTasks.AddUnifiedGroupLinks
    + PSComputerName        : outlook.office365.com

Instant as a owner also fails, like the manual also reffers:

PS C:\WINDOWS\system32> Add-UnifiedGroupLinks -Identity "Klas_1A1F_22-23" -LinkType "Owners" -Links "a****es.desa*****r@***.be"
Only Members can be Owners of a group. Please add 'annelies.desaeger' first as members before adding them as owners.
    + CategoryInfo          : NotSpecified: (Klas_1A1F_22-23...5a-2170b2539977:ADObjectId) [Add-UnifiedGroupLinks], ADNotAMemberException
    + FullyQualifiedErrorId : [Server=PA4PR03MB6992,RequestId=1ee4f0e9-aa80-4903-9702-c9bca9ef625d,TimeStamp=13/12/2022 20:28:59] [FailureCategory=Cmdlet-ADNotAMemberException] 9B67D48,Micros
   oft.Exchange.Management.RecipientTasks.AddUnifiedGroupLinks
    + PSComputerName        : outlook.office365.com

Anyone can help?

Thx!

1

There are 1 best solutions below

0
Minkus On

You can do this in Microsoft Graph:

Connect-MgGraph -Scopes "Group.ReadWrite.All"

$MgGroup = Get-MgGroup -Filter "DisplayName eq 'Your Group Name'"
$MgUser = Get-MgUser -Filter "UserPrincipalName eq '[email protected]'"

New-MgGroupOwner -GroupId $MgGroup.Id -DirectoryObjectId $MgUser.Id

Or as a one liner:

Connect-MgGraph -Scopes "Group.ReadWrite.All"

New-MgGroupOwner -GroupId (Get-MgGroup -Filter "DisplayName eq 'Your Group Name'").Id -DirectoryObjectId (Get-MgUser -Filter "UserPrincipalName eq '[email protected]'").Id

https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.groups/new-mggroupowner?view=graph-powershell-1.0