Jul 15 2018
10:25 PM
- last edited on
Feb 01 2023
11:56 AM
by
TechCommunityAP
Jul 15 2018
10:25 PM
- last edited on
Feb 01 2023
11:56 AM
by
TechCommunityAP
Hi,
May I ask for your assistance to edit this script from Microsoft instead of checking every group that a user is member of maybe I could use the variable $filter in removing the user also?
Best Regards,
Mark
Jul 15 2018 11:07 PM
You can combine the two scripts as follows. Here you search the groups where the user is member and then removes the user from those groups.
$email= read-host -prompt "user@domain.com"
$Mailbox=get-Mailbox $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter | %{Remove-DistributionGroupMember -identity $_.name -Member $email -confirm:$false}
Get-UnifiedGroup -ResultSize Unlimited -Filter $Filter | %{Remove-UnifiedGroupLinks -Identity $_.name -LinkType Members -Links $email -Confirm:$false}
Jul 16 2018 12:05 AM
Jul 16 2018 12:44 AM
For that scenario, use the first script as-is: it will save the groups in two csv files.
The you can simply read the files and delete user from those groups. The script below should do the trick, but I didn't had time to test it.
$email= read-host -prompt "user@domain.com"
Import-CSV "C:\Scripts\TestDL.csv" | %{Remove-DistributionGroupMember -identity $_.name -Member $email -confirm:$false}
Import-CSV "C:\Scripts\TestUG.csv" | %{Remove-UnifiedGroupLinks -Identity $_.name -LinkType Members -Links $email -Confirm:$false}
Jul 16 2018 09:56 AM
I just recently published a script that does just that: https://gallery.technet.microsoft.com/Remove-user-from-all-8d44f772?redir=0
Use it with the -Verbose and -WhatIf parameter first to preview the exact changes that will be made, once you are satisfied with the results run it for real.
Jul 16 2018 10:46 PM
Hi Nestori,
Tried this but seem it doesn't work, I like this code as it is simple.
@Nestori Syynimaa wrote:You can combine the two scripts as follows. Here you search the groups where the user is member and then removes the user from those groups.
$email= read-host -prompt "user@domain.com"
$Mailbox=get-Mailbox $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter | %{Remove-DistributionGroupMember -identity $_.name -Member $email -confirm:$false}
Get-UnifiedGroup -ResultSize Unlimited -Filter $Filter | %{Remove-UnifiedGroupLinks -Identity $_.name -LinkType Members -Links $email -Confirm:$false}
Best Regards
Mark Diaz
Jul 17 2018 12:42 AM
Hi Mark,
Did you got some error message? You need to connect to Exchange Online before running the script (see for example http://o365blog.com/powershell)
Jul 17 2018 02:50 AM
Hi Nestori,
It is now working Many Thanks !
@Nestori Syynimaa wrote:Hi Mark,
Did you got some error message? You need to connect to Exchange Online before running the script (see for example http://o365blog.com/powershell)
Best Regards,
Mark Diaz