Solved

PowerShell command to add users to a DL

Posted on 2010-08-24
26
1,589 Views
Last Modified: 2012-05-10
I have to add 350+ users to a DL. I know there is a PowerShell command to do this but I can't get it to work. Below is what I am using. I found it in a Microsoft article. I want to pull users from a .csv file. Need help! The command below is having issues with the code in bold. Just need the proper syntax.

new-distributiongroup -alias avalanche -name "Avalanche Team" -type distribution -org users -SamAccountName AvalancheTeamimport-csv Avalanche.csv | foreach {add-distributiongroupmember avalanche -member "avalanche$($_.No)"}
0
Comment
Question by:osiexchange
  • 12
  • 12
  • 2
26 Comments
 
LVL 14

Expert Comment

by:Shabarinath Ramadasan
ID: 33510309
Break it into two parts.
Create a Distribution list first.

Then

$users = get-content “List of users”

Foreach($user in $users)

{

    Add-qadgroupmember –Identity “CN=GroupName,OU=OUName,DC=DCName” –Member $user

}

Hope this helps.

Good luck
Shaba
0
 

Author Comment

by:osiexchange
ID: 33510395
Is there a typo here? It does not like the command.

Add-qadgroupmember
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33510515
new-distributiongroup -alias avalanche -name "Avalanche Team" -type distribution -org users -SamAccountName AvalancheTeam

import-csv Avalanche.csv | foreach-object { add-distributiongroupmember avalanche -member $_.No }

the $_.No is the column is the csv file you are using
0
 

Author Comment

by:osiexchange
ID: 33510623
Thanks. I am a real noob when it comes to this. Was does the No stand for? If I am going to use the first column in the .csv file, what would it look like?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33510642
use the column header name
so if cell A1 = Name it would be $_.Name
0
 

Author Comment

by:osiexchange
ID: 33511101
Thanks. That worked. My spreadshee has the first and last name spread across two columns, A1 and B1. How would I go about specifying an additional column in the above command, or can I?
0
 
LVL 14

Expert Comment

by:Shabarinath Ramadasan
ID: 33511128
get-qadgroupmember is a commandlet from quest.
http://www.quest.com/powershell/activeroles-server.aspx

Thanks
Shaba
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33511216
import-csv Avalanche.csv | foreach-object { add-distributiongroupmember avalanche -member ($_.First + " " + $_.Last }
0
 

Author Comment

by:osiexchange
ID: 33511490
With this command, its combining the firstname and last name together with no space. For example, the firstname column is John and the lastname column is Doe. The command comes back with an error stating it cannot find a user named JohnDoe. Note, no space. I am guessing the "" is supposed to input a space but it does not work. Also, I think the above command is missing a ")" symbol after  $_.Last? I am very close but no luck. As I said, I am a noob at this so anything you provide will be helpful.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33511510
the example i gave earlier was missing the ), good catch there is also a space between the "" so it is ($_.First + " " + $_.Last)
0
 

Author Comment

by:osiexchange
ID: 33512364
It did not like the " ". Below is the error I got.

Add-DistributionGroupMember : Object " " could not be found. Please make sure that it was spelled correctly or specify a di
ferent object.

This is what I am typing:
import-csv testdl.csv | foreach-object { add-distributiongroupmember testmike -member ($_.First + " " + $_.Last)}
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33512411
then use the following:

import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  add-distributiongroupmember testmike -member $name }
0
 

Author Comment

by:osiexchange
ID: 33513165
Same error. Not sure what I am doing wrong. I copied and pasted your command into powershell.

Add-DistributionGroupMember : Object " " could not be found. Please make sure that it was spelled correctly or specify a different object.
At line:1 char:104
+ import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  add-distributiongroupmember <<<<  testmike -member $name }

    + CategoryInfo          : NotSpecified: (0:Int32) [Add-DistributionGroupMember], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : C40E2DA7,Microsoft.Exchange.Management.RecipientTasks.AddDistributionGroupMember
0
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
LVL 32

Expert Comment

by:endital1097
ID: 33513243
it is not finding the group you are specifying
0
 

Author Comment

by:osiexchange
ID: 33513268
From the error, it looks like it has an issue with the Object " ".

Add-DistributionGroupMember : Object " " could not be found. Please make sure that it was spelled correctly or specify a different object.
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33513373
one sec, going to run it in my lab for verification
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33513428
i ran the exact same script in my lab and it was successful

import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  add-add-distributiongroupmember testmike -member $name }

the <<<< shows where in the command it failed, and in this instance it is after the add-distributiongroupmember

i would run get-distributiongroup testmike
to verify that the group can be located
0
 

Author Comment

by:osiexchange
ID: 33513842
See below. I am using PowerShell 2.0 if it makes a difference. To me,it looks like its still complaining about the double quotes ""

 Object " " could not be found.

[PS] C:\>Get-DistributionGroup testmike

Name                            DisplayName                    GroupType
----                            -----------                    ---------
Testmike                        Testmike                       Universal
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33513877
i was also using Powershell 2.0

so let's troubleshoot
update the script so it looks like the following:

this should output the names "first last"
import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  $name }

Open in new window

0
 

Author Comment

by:osiexchange
ID: 33514005
That did work ok.
0
 

Author Comment

by:osiexchange
ID: 33514129
Woud it be easier if I just copied the first two columns from Excel into notepad and save it as a simple .txt file?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33514144
that shows that the $name variable is being created as needed
now let's see it output the name then try to add the member
import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  $name; Add-DistributionGroupMember Testmike -Member $name }

Open in new window

0
 
LVL 32

Expert Comment

by:endital1097
ID: 33514176
it's the same, either way will work
0
 

Author Comment

by:osiexchange
ID: 33514414
OK. I copied both columns to a notepad .txt file. Firstname Lastname with a space in the middle. I am afraid to mess things up. I have over 350 names but I will test it with just one or two. What would be the command to import all these users assuming the file name is users.txt and the DL name is testDL. Thanks for all your help with this. I bumped the points to 500.
0
 
LVL 32

Accepted Solution

by:
endital1097 earned 500 total points
ID: 33514540
with a txt file it changes to the following

$users = Get-Content testdl.txt

foreach ($u in $users) { add-distributiongroupmember testmike -member $U }

Open in new window

0
 

Author Closing Comment

by:osiexchange
ID: 33515462
Many thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
This video discusses moving either the default database or any database to a new volume.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now