Removing contact from all mail groups I want to remove the mail contact from all Groups in the "Member Of" tab. I am able to remove all MemberOf for mail contact. How can I improve the speed of the script?

Get-MailContact &lt;name&gt; | ForEach-Object { Get-ADObject $_.distinguishedname -Properties memberof } | Remove-ADPrincipalGroupMembership -Identity $_ -MemberOf $_.memberof
This seems pretty optimal to me given how simple it is, only a question, why the foreach? Do you expect Get-MailContact to return multiple contacts ?
I don't have an AD I can test it against, but I'd be curious to know if &quot;reversing&quot; the operation by removing the member from the groups instead of removing the membership from the user could make it faster:
$ADContact = Get-MailContact &lt;name&gt; | Get-ADObject -Properties memberof 
# Loop through all the groups and remove the user from them.
foreach ($group in $ADContact.MemberOf) {Remove-ADGroupMember -Identity $group -Member $ADContact}