Add Users to Security Group in Exchange Online

I'm trying to add users from a csv file to a cloud-based Security group. I understand I need the Objectid of the SG. However I get 'access denied' when calling Get-Msolgroup -isAgentRole -UserprincipalName I tried the steps mentioned in the following link:

My csv has only the UserPrincipalName column of users. Any thoughts?
Anthony K O365Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
The IsAgentRole parameter is only used when you are a partner, with the corresponding permissions. To get the ObjectId of a group, simply use:

Get-MsolGroup -SearchString "name of the group" | select DisplayName,ObjectId

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony K O365Author Commented:
Anthony K O365Author Commented:

After running the PS all day, I get this error message:

Add-MsolGroupMember : You cannot update mail-enabled groups using this cmdlet. Use Exchange Online to perform this operation.

At C:\Users\AddMemberToSG.ps1:8 char:12
$sub2 | % {Add-MsolGroupMember -groupObjectid ‘xxxxx-xxxxxx-xxxxxxxx-xxxxxxx’ -GroupMemberObjectId $_.ObjectId -GroupMemberType User}

Any thoughts? Thank you!
Vasil Michev (MVP)Commented:
As noted in the error message, this cmdlet will only work against "regular" security groups. For mail-enabled security groups, use the Exchange remote PowerShell and Add-DistributionGroupMember
Anthony K O365Author Commented:
Excellent! This worked. I truly appreciate your help. True MVP
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.