Help combining script with Import-CSV to execute on a list of emails

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 = ""

$mailbox = Get-Mailbox -Identity $email


$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?

Welcome to the forum. :wave:t4:

… and you did not find something helpful about loops in PowerShell? :smirk:

If you’re satisfied with the code you have so far you simply wrap it in a loop where you iterate over all your email addresses you have in your CSV file.

Assumed your CSV file has a header “email” it’s basically something like this:

$EmailList = Import-Csv -Path "path to your input CSV file" -Delimiter ','

foreach ($email in $ {
    "do something meaningful with the email address '$($email)'"    

Small modification to Olafs code, make sure you reference the header

$EmailList = @"
"@ | ConvertFrom-Csv

foreach ($email in $EmailList) {
    "do something meaningful with the email address '$($email.Email)'"    

1 Like

Rob is totally right … I missed that. I corrected my code suggestion slightly. This way you would not need to change your part of the code. :wink:
Thanks Rob. :+1:t4:

Big thanks to Olaf and Rob for your input on this! I got it working as intended now. I greatly appreciate the help from both of you! Keep on shellin’!