Remove users from D.G

Hello,

Please, I need a Script (Powershell or VBS) to Remove a list of Users (csv file) from all Distribution Groups. Indeed, Remove several users are member of different distribution groups from Active directory.

File.csv contain just the sAMAccountName

for exemple :
sAMAccountName
User1
User2
User3
User4
...

Thanks for all your help.
Best regards,
DRRAMAsked:
Who is Participating?
 
CodeTwo SoftwareConnect With a Mentor Software DeveloperCommented:
You could try the following script:
$dgs = Get-DistributionGroup
$users = Import-Csv <your-csv-file>
foreach ($dg in $dgs) {
foreach ($user in $users) {Remove-DistributionGroupMember -id $dg -member $user.SamAccountName
}
}

Open in new window

If you are certain you want to remove those distribution group members, add the -confirm $false to the script
0
 
DRRAMAuthor Commented:
Thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.