Forum Discussion
sofo-83
Mar 30, 2022Copper Contributor
add just one user to many groups
Hi please help me to make a script in PowerShell to add just one user to many groups
sofo-83
Apr 05, 2022Copper Contributor
Apr 08, 2022
Did this fix your problem?
- Apr 11, 2022Ok.. It's a mail-enabled group that you want to update...
Install-Module ExchangeOnlineManagement (If you don't already have it installed)
Connect-ExchangeOnline
foreach ($group in "sg-retail", "sg-hr", "sg-legal") {
Add-DistributionGroupMember -Identity $group -Member 'useremailaddress'
} - sofo-83Apr 11, 2022Copper Contributor
- Apr 11, 2022I think this is the syntax inside the foreach loop:
$groupid=Get-MsolGroup | Where-Object DisplayName -Match $group
Add-MsolGroupMember -GroupObjectId $groupid.objectid -GroupMemberType User -GroupMemberObjectId $usertobeaddedid.ObjectId
(https://docs.microsoft.com/en-us/powershell/module/msonline/add-msolgroupmember?view=azureadps-1.0) - sofo-83Apr 11, 2022Copper Contributor
- sofo-83Apr 11, 2022Copper Contributor
Sorry but regarding the company policy I can't share the group's name and ids
I appreciate your help 🙂
I have changed my script to :foreach ($group in "it-test1","IT-test3") {
Get-MsolGroup | Where-Object DisplayName -Match $groupAdd-MsolGroupMember -ObjectId $groupid.objectid -RefObjectId $usertobeaddedid.ObjectId
}
but the error now
I did that becase I found that Get-MsolGroup return all the groups
- Apr 11, 2022Your screenshot does not show that?
- sofo-83Apr 11, 2022Copper Contributor
- Apr 11, 2022You are connected using connect-azuread? Get-azureadgroup does return groups?
- sofo-83Apr 11, 2022Copper Contributor
- Apr 11, 2022In this case... $group is indeed IT-test3, because it's the last one in the foreach loop. $groupid however should return two items, does it?
- sofo-83Apr 11, 2022Copper Contributor
- Apr 09, 2022Ok, if you need help or have an error message.. Let us know
- sofo-83Apr 09, 2022Copper Contributor
Unfortunately no, I think the script you wrote is perfect but the problem is on my side .
I'm still trying to find out what is wrong