Forum Discussion
add just one user to many groups
Thanks a lot
is the group name must be as a string (name) or should I bring the object ID? Harm_Veenstra
- Apr 05, 2022
The name of the group as you see it in Active Directory, Display Name I guess. Let me know if it worked out for you, you can mark my answer as solution to mark it as solved if it does
- sofo-83Apr 05, 2022Copper ContributorForEach ($Group in $Groups) {Add-AzureADGroupMember -ObjectId "username" -MemberOf $Group}
I have changed the parameter Add-ADPrincipalGroupMembership to Add-AzureADGroupMember
but still get error for the parametr -MemberOf .
Add-AzureADGroupMember : A parameter cannot be found that matches parameter name 'MemberOf'.- Apr 05, 2022
sofo-83 This works for me:
$usertobeaddedid=Get-AzureADUser -ObjectId 'usermailaddress' $groups=@("sg-retail", "sg-hr", "sg-legal") foreach ($group in $groups) { $groupid=Get-AzureADGroup | Where-Object DisplayName -Match $group Add-AzureADGroupMember -ObjectId $groupid.objectid -RefObjectId $usertobeaddedid.ObjectId }
If the user is already a member, you will receive this error:
Add-AzureADGroupMember : Error occurred while executing AddGroupMember
Code: Request_BadRequest
Message: One or more added object references already exist for the following modified properties: 'members'.
RequestId: 6c01973a-aad9-4396-b89a-0b8fa62d09b4
DateTimeStamp: Tue, 05 Apr 2022 13:27:54 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:3 char:1
+ Add-AzureADGroupMember -ObjectId $groupid.objectid -RefObjectId $user ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-AzureADGroupMember], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.AddGroupMember
- sofo-83Apr 05, 2022Copper Contributorthe script doesn't fit with Azure AD 😞