Hello! Thank you in advance to anyone who takes the time to read my post. I am very much a powershell noob so I really appreciate any help that is offered! I’ve been spending a lot of time googling but not quite achieving what I want with this script.
So I am working on this script to essentially remove users from all distribution groups that they’re a part of. I was able to find a script that works great for what I want, but it’s on an individual basis. What I mean by that is I have to put the user’s email into one part each time, run the script, then the same for the next user’s email, and so on until I take care of all who need it.
Now what I’m trying to do is take that working script and combine it with a CSV file that would have the emails for each person in need of this being done to and run it upon each email address entered into the CSV. Only thing is I’m having a hell of a time figuring out how to properly format this to get it do what I want.
The working script I found that works now, without using a CSV, looks like below:
$email = "xxxxxx@domain.com"
$mailbox = Get-Mailbox -Identity $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
$DistributionGroupsList = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter
Write-host `n
Write-host "Listing all Distribution Groups:"
Write-host `n
$DistributionGroupsList | ft
ForEach ($item in $DistributionGroupsList) {
Remove-DistributionGroupMember -Identity $item.DisplayName –Member $email –BypassSecurityGroupManagerCheck -Confirm:$false
}
Write-host `n
Write-host "Successfully removed"
Remove-Variable * -ErrorAction SilentlyContinue
Like I said this works exactly as I want, but I have to put the email in manually at that first line, then repeat that for each email that also needs this done. How can I parse this out to import the CSV with all the emails in it and run it on each of those emails in the CSV?