Exchange 从所有组中移除指定账号(执行有错)

第1种:Microsoft 365: PowerShell to Remove a user from All Distribution Groups – SharePoint Diary

  • $UserToRemove= "Steve@Crescent.com"
  • Try {
  •     #Connect to Exchange Online
  •     #Connect-ExchangeOnline
  •     #Get All Distribution Lists - Excluding Mail enabled security groups
  •     $DistributionGroups= Get-Distributiongroup-resultsizeunlimited |  Where {!$_.GroupType.contains("SecurityEnabled")}
  •     #Loop through each Distribution Lists
  •     ForEach($Group in $DistributionGroups)
  •     {
  •         #Check if the Distribution List contains the particular user
  •         If((Get-DistributionGroupMember$Group.Name | Select-ExpandPrimarySmtpAddress) -contains $UserToRemove)
  •         {
  •             Remove-DistributionGroupMember -Identity $Group.Name-Member $UserToRemove -Confirm:$false
  •             Write-host"Removed user from group '$Group'"-fGreen
  •         }
  •     }
  • }
  • Catch {
  •     write-host -f Red "Error:"$_.Exception.Message
  • }

第2种

$DGs= Get-DistributionGroup foreach( $dg in $DGs) { Remove-DistributionGroupMember $dg -Member “user@domain.com” }