user8776656
user8776656

Reputation: 103

Powershell 365 - Remove email from distribution group

$Email = "@"
Get-DistributionGroup | where { (Get-DistributionGroupMember $_.Name | foreach {$_.PrimarySmtpAddress}) -contains "$Email"}

Results: Name, DisplayName, Group Type, PrimarySMTPAddress

I need to remove the user's email address from the distribution group. I know it will be a foreach command.

Does anyone know how to run that command?

Upvotes: 0

Views: 691

Answers (1)

infosecb
infosecb

Reputation: 129

You can use the If statement within a For loop that determines if the email address exists in the distribution group.

If this condition is satisfied, that's when you want to run the Remove-DistributionGroupMember cmdlet:

$Email = "@"

Get-DistributionGroup | 
ForEach-Object { 
    If ((Get-DistributionGroupMember -Identity $_.Name).PrimarySmtpAddress -contains $Email) { 
        Remove-DistributionGroupMember -Identity $_.Name -Member $Email -WhatIf}
    } 

}

Upvotes: 1

Related Questions