Remove user from Office 365 Groups

Hi,
I need a powershell script to remove user from all groups, Distribution & Office 365.
I have the following code, works fine for Distribution, but not for Office 365 groups...
Can someone Please Help?

$name = Read-Host -Prompt "FirstName.LastName"
$email = "@abc.com"
$user = $name + $email

$mailbox=get-mailbox $user

					$dgs= Get-DistributionGroup
                    $ugs= Get-UnifiedGroup
					foreach($dg in $dgs){
					    $DGMs = Get-DistributionGroupMember -identity $dg.Identity
					    foreach ($dgm in $DGMs){
					        if ($dgm.name -eq $mailbox.name){
					       
					            write-host 'User Found In Group' $dg.identity
					              Remove-DistributionGroupMember $dg.Name -Member $user -Confirm:$False 
					        }
					    }
					}

					foreach($ug in $ugs){
					    $UGMs = Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Members | foreach {$_.name}) -contains $mailbox.Alias}
					    foreach ($ugm in $UGMs){
					        if ($ugm.name -eq $mailbox.name){
					       
					            write-host 'User Found In Group' $ug.identity
					              Remove-UnifiedGroupLinks -identity $UGMs.alias -LinkType Member -Links $mailbox -Confirm:$False 
					        }
					    }
					}

Open in new window

LVL 11
AcklesAsked:
Who is Participating?
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:
You are using an incorrect syntax to build the $UGMs variable, $_.Alias will not resolve within the pipeline unless you put a foreach statement first. Anyway, there's a much easier/faster method to get the groups of which particular user is a member:

Get-Recipient -Filter "Members -eq 'CN=user,OU=tenant.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=EURPR03A001,DC=prod,DC=outlook,DC=com'" 

Open in new window


Simply get the user's DN and replace it in the above. You can use the -RecipientTypeDetails parameter to filter out just DGs or just Office 365 Groups and then use the corresponding cmdlet to remove the user accordingly.
AcklesAuthor Commented:
Sorry, but can you please modify my script?
AcklesAuthor Commented:
Any Help?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Vasil Michev (MVP)Commented:

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
AcklesAuthor Commented:
Thanks Vasil,
I tried the script, it does job for DG, but for O365 groups it says, No matching security Groups found for user.... Skipping?
AcklesAuthor Commented:
My bad. Works perfectly!!!
Thanks a lot
AcklesAuthor Commented:
Awesome
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
Powershell

From novice to tech pro — start learning today.