Distribution Lists

Hello,

Powershell Script to Remove list of Users from all Distribution Lists
Please, I need a script (Powershell or vbs), I can use it to remove a list of users (csv file) from all of distribution lists they are a part of.

Thanks for all your help.
DRRAMAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DRRAMAuthor Commented:
Please, I am waiting for your help
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
That gonna be something like:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
$identity = "<Name> or <displayName>"
get-DistributionGroup | %{
    $group = $_
    Get-DistributionGroupMember -Identity $group| %{
        if($_.Name -match $identity){
            Write-Host "removing ""$($_.Name)"" from group $group"
            Remove-DistributionGroupMember -Identity $group -Member $_.Name -Confirm:$false
        }
        <#else{
            ##the user is not part of the group user $_.name and groupname in $group
        }#>
    }
}

Open in new window


For a single user in $identity variable.
0
DRRAMAuthor Commented:
No beacause, I need to remove a list of users (csv file) from all of distribution lists they are a part of.

Indeed, several users have different distribution lists
after we run the script the users didn't have any distribution lists.

Best regards,
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
Ok but give the whole information then.
What is the content of the csv? post a dummy file at least.

So you can have what you want.
Does it contain the name and last name, the samaccountname, or the display name?
What are the fields in your csv.

Jose.
0
DRRAMAuthor Commented:
Yes , excuse me

File.csv contain just the sAMAccountName

for exemple :
sAMAccountName
User1
User2
User3
User4
...

Thx
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DRRAMAuthor Commented:
Thanks,
Please what is technical requirements to run the script

We can specify in the script the name of Domain controler and the path : ou=Site,DC=domain,DC=com

Best regards,
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
Just run it.
In the page is how to run it.

1. Set-executionpolicy unrestricted
2. Scriptname.Ps1-csvpath input.Csv. For samaccountname.

You're asking to run the script across an organizational unit and a domain controller this doesn't have any sense since your samaccountname is unique in the domain and those parameters are not useful for your purpose
0
DRRAMAuthor Commented:
Please Jose Gabriel;

I need this "We can specify in the script the name of Domain controler and the path : ou=Site,DC=domain,DC=com"
because when I run the script we have the followinf warning :

WARNING: By default, only the first 1000 items are returned. Use the ResultSize
 parameter to specify the number of items returned. To return all items,
specify "-ResultSize Unlimited". Be aware that, depending on the actual number
of items, returning all items can take a long time and consume a large amount
of memory. Also, we don't recommend storing the results in a variable. Instead,
 pipe the results to another task or script to perform batch changes.


Best regards,
Rami.
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
OK, my answer is there, they use samaccountname that you provided in the CSV.
you didn't specify the number of the enterprise so I assumed that it was small.
I'll do the changes in my script related to the number of users that can return.

Once again, if you have the samaccountname, you don't need to specify the OU, the command is intelligent enough to search the samaccountname, and relative to the domain controller and assuming is a single domain controller, not multiple domain controllers, this will be my final answer.

Update:
You have all the SamAccountName in a CSV file, there's no need at all to use the  "-resultsize  -Unlimited" because the script is taking the SamAccountName, to find out the Display Name and then use the displayName to remove the users from all the Distribution Lists (one by one). So if your csv is well structured there's no need to do anything else.  That's all from my part in this question.
The only thing saving in a variable is the Group Name and the Name. The  posted script is free and you can change it if you think it doesn't fit what you want
0
DRRAMAuthor Commented:
Thanks Jose and sorry if I bother you; I understood and I agree with you

Another way, "distribution list in exchange = distribution group in active directory"

We can remove list of users (csv file) from all of distribution group they are a members in active directory. Indeed, several users are member of different distribution group

If you want, I create another ticket for this request

Best regards,
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
There are 2 kinds of "groups" in AD, Security groups and Distribution Groups (the distribution groups, universal) are the one that uses exchange as "distribution group" and nope, this will only work on distribution groups on exchange server.

There are 3 scopes for "security" or "distribution" groups in AD, if you pick global or domain local, you won't see it on exchange. so Distribution group in AD != Distribution group in Exchange.

Jose.
0
DRRAMAuthor Commented:
Thx
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.