Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1778
  • Last Modified:

PowerShell command to add users to a DL

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
osiexchange
Asked:
osiexchange
  • 12
  • 12
  • 2
1 Solution
 
Shabarinath RamadasanInfrastructure ArchitectCommented:
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
 
osiexchangeAuthor Commented:
Is there a typo here? It does not like the command.

Add-qadgroupmember
0
 
endital1097Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
use the column header name
so if cell A1 = Name it would be $_.Name
0
 
osiexchangeAuthor Commented:
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
 
Shabarinath RamadasanInfrastructure ArchitectCommented:
get-qadgroupmember is a commandlet from quest.
http://www.quest.com/powershell/activeroles-server.aspx

Thanks
Shaba
0
 
endital1097Commented:
import-csv Avalanche.csv | foreach-object { add-distributiongroupmember avalanche -member ($_.First + " " + $_.Last }
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
the example i gave earlier was missing the ), good catch there is also a space between the "" so it is ($_.First + " " + $_.Last)
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
then use the following:

import-csv testdl.csv | foreach-object { $name = $_.First + " " + $_.Last;  add-distributiongroupmember testmike -member $name }
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
it is not finding the group you are specifying
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
one sec, going to run it in my lab for verification
0
 
endital1097Commented:
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
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
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
 
osiexchangeAuthor Commented:
That did work ok.
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
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
 
endital1097Commented:
it's the same, either way will work
0
 
osiexchangeAuthor Commented:
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
 
endital1097Commented:
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
 
osiexchangeAuthor Commented:
Many thanks.
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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 12
  • 12
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now