Learn how to a build a cloud-first strategyRegister Now

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

Add a list of users to a Distribution group in AD 2008 / Exchange 2003 from a .csv with PowerShell

I've found a bunch of examples that come close to what I'm trying to do but I haven't been able to figure out how to tweak them to do what I need.  Basically I've got a .csv file with a single column containing usernames.  I'd like to automate adding these users to a single group in AD.  Any help / code anyone could offer (an explanation would be very appreciated too) would be great.  I've checked out this post: http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_24436457.html?sfQueryTermInfo=1+add+qadgroupmemb which comes close...

Thanks
Josh
0
josh_lohff
Asked:
josh_lohff
  • 6
  • 5
  • 3
1 Solution
 
netcmhCommented:
rather than reinvent the wheel, here's a link: http://dmitrysotnikov.wordpress.com/2009/12/23/adding-a-list-of-users-to-a-group/

Hope it helps
0
 
josh_lohffAuthor Commented:
Thanks for the link, that works as an individual command but doesn't address importing users from a .csv file.  Attached are the results when I substitute the comma-separated values w/an Import-Csv command.

Thanks
Josh
[PS] C:\Users\jlohff\Desktop>Import-Csv C:\Users\jlohff\Desktop\abc1.csv -split "`n" | Add-QADGroupMember abc
Import-Csv : A parameter cannot be found that matches parameter name 'split'.
At line:1 char:51
+ Import-Csv C:\Users\jlohff\Desktop\abc1.csv -split <<<<  "`n" | Add-QADGroupMember abc
    + CategoryInfo          : InvalidArgument: (:) [Import-Csv], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ImportCsvCommand

Open in new window

0
 
netcmhCommented:
use the foreach qualifier
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
netcmhCommented:
Also, if it's a csv, then you need  -split "," and not -split "`n"
0
 
josh_lohffAuthor Commented:
OK, here's what I tried: Import-Csv C:\Users\jlohff\Desktop\abc1.csv -Delimiter "," | ForEach-Object {Add-QADGroupMember abc}.  I can't get the '-split' option to work but Delimeter looks like it may accomplish the same thing (or not?).  When I run the command it just returns down to the next line and doesn't update my group.

Thanks
Josh
[PS] C:\Users\jlohff\Desktop>Import-Csv C:\Users\jlohff\Desktop\abc1.csv -Delimiter "," | ForEach-Object {Add-QADGroupMember abc}
[PS] C:\Users\jlohff\Desktop>

Open in new window

0
 
netcmhCommented:
Try this
[PS] C:\Users\jlohff\Desktop>Import-Csv C:\Users\jlohff\Desktop\abc1.csv -Delimiter "," | ForEach-Object -process {Add-QADGroupMember abc}

Open in new window

0
 
netcmhCommented:
not sure, but would you need -NewParentContainer ? as in -NewParentContainer  domainname.com\abc
0
 
josh_lohffAuthor Commented:
I get the same results with abc & domain.com\abc (just a return to the prompt).  Also, adding the '-process' switch didn't seem to change anything.  In case this is the issue, here's the contents of my .csv file:  user1,user2,user3
0
 
Chris DentPowerShell DeveloperCommented:

Hmm you said column, that would be:

UserName
user1
user2
user3

That would lend itself well to Import-CSV. However, if it's really "user1,user2,user3" on a single line you'd be better using Get-Content for this.

For example:

Get-Content "WhateverYourFileIsCalled.txt" | %{
  $_.Split(",") | %{ Add-QADGroupMember "SomeGroupName" -Member $_ }
}

If that doesn't work, showing us a sample of the file as you have it now would help a lot :)

Chris
0
 
josh_lohffAuthor Commented:
Sorry about the confusion on the file.  Your example with the column is how it was originally setup and how I'd like to make it work.  When you made the distinction with that delimeter I opened the .csv in notepad to check and realized it didn't actually have any commas in it.  I'm not sure if that's standard or just some excel goofiness but that's the point where I changed it over to have commas.  At this point, I'm using a .csv file with a single column and label at the top (attached).  When I run the command you specified (also attached), I receive the following:

cmdlet Add-QADGroupMember at command pipeline position 1
Supply values for the following parameters:
Member[0]:
Import-Csv C:\Users\jlohff\Desktop\abc1.csv -Delimiter "," | ForEach-Object -process {Add-QADGroupMember abc}
cmdlet Add-QADGroupMember at command pipeline position 1
Supply values for the following parameters:
Member[0]:

Open in new window

abc1.csv
0
 
Chris DentPowerShell DeveloperCommented:

Excellent, so what we'd need to use the file above is this:

Import-CSV abc1.csv | %{ Add-QADGroupMember "Group Name" -Member $_.Username }

% is an alias for ForEach-Object, that hasn't gone away.

Chris
0
 
josh_lohffAuthor Commented:
IT WORKED!  Thank you so much Chris.  I figured it would be something simple like this.  I wasn't making the connection between the *_.Username and header of the column.  I'm guessing those two need match?

Thanks again
Josh
0
 
Chris DentPowerShell DeveloperCommented:

Indeed they do.

Take the first part of the script:

Import-CSV abc1.csv

That takes your file and converts it into a collection of Objects. Each object has a property matching each of the columns in the CSV file (obviously only one column here). Running it on its own will show the CSV file as a table within the console.

For each entry in the object, we can access the value in that property using the property name.

PowerShell is a bit of an explorers scripting language. Don't hesitate to break up scripts into their component parts. Get-Help and Get-Member are invaluable in the discovery process, they help remove the reliance on external documentation and existing samples.

Chris
0
 
josh_lohffAuthor Commented:
Well, thanks again.  I've been struggling with this on and off all day and it's nice to have figured out, and I'll be sure to leverage Get-Help and Get-Member in the future.  I'll definitely use it again.  

Have a good one
Josh
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now