Adding list of users to group

Say, you get an email asking to add a bunch of members to a distribution list you manage. What is the easiest way to do this? Going to Outlook, making 5 clicks to get to the dialog box, and then manually adding each user from the address book picker is definitely not fun. However, PowerShell definitely is.

I’ve seen people submitting lists of members to add in a couple of ways: separated by commas (or semicolons) or each on a separate line. Both would work fine – the only difference is how you would tell PowerShell to split this string into actual members’ names.

Let’s start with a comma-separated list. Like this:

Hey Dmitry,

Could you please add Kirk Munro, Darin Pendergraft, Oleg Shevnin to the PowerGUI DL?

Thanks!

All you need to do, is copy the part of the email with the names, use PowerShell -split operator to break it by comma characters, and pipe the result into Add-QADGroupMember:

'Kirk Munro, Darin Pendergraft, Oleg Shevnin' -split "," | Add-QADGroupMember PowerGUI

That is it!

Line by line option is not much different – you just have to split by newline character ("`n") instead of comma:

'Kirk Munro
Darin Pendergraft
Oleg Shevnin' -split "`n" | Add-QADGroupMember PowerGUI

And by the way, email addresses instead of user names are totally fine too:

'user1@domain.my, user2@domain.my' -split "," | Add-QADGroupMember PowerGUI

One of those cases when command line is so much easier than UI.🙂

14 Responses to “Adding list of users to group”


  1. 1 Jack December 24, 2009 at 2:45 am

    Another best practice on powershell, i like it!

  2. 2 Mihail Stacanov December 24, 2009 at 9:26 am

    Will should be careful to use ‘Kirk Munro, Darin Pendergraft, Oleg Shevnin’ -split “,” because two people Darin Pendergraft and Oleg Shevnin will be starting from space.
    You should use ‘Kirk Munro, Darin Pendergraft, Oleg Shevnin’ -split “, ” instead of

  3. 3 Dmitry Sotnikov December 24, 2009 at 12:29 pm

    Mihail, actually in my experiments spaces were not an issue and Add-QADGroupMember was working just fine even with speces preceding the names. Did you have any issues?

    • 4 Mihail Stacanov December 24, 2009 at 10:00 pm

      No, i had no problems but when i piped out the output after split it was with space🙂

      • 5 Dmitry Sotnikov December 25, 2009 at 10:34 am

        Yes, that for sure. Split leaves the spaces (unless you explicitly tell it not to :)) but then Add-QADGroupMember seems to be smart enough to handle them.

  4. 6 net January 7, 2010 at 9:22 pm

    What about importing from a csv file?

    This wouldn’t work:

    Import-Csv C:\temp\list.csv -Delimiter “,” | ForEach-Object -process {Add-QADGroupMember Group}

    Anyone?

    • 7 Dmitry Sotnikov January 8, 2010 at 7:23 pm

      Import-CSV produces objects with members called the way your columns are – not strings.

      If your CSV file is a simple text file with a username per line, use get-content instead – this will give strings:

      Get-Content C:\temp\list.txt | Add-QADGroupMember MyGroup

      If you do have a CSV file and the username or email address or some other unique identifier as a column – use $_.ColumnName explicitly, e.g.:

      Import-Csv C:\temp\list.csv -Delimiter ‘,’ | ForEach-Object -process {Add-QADGroupMember -Identity MyGroup -Member $_.Username}

      If you still have trouble – please post your question to the forums at http://powergui.org

  5. 8 Andriy Dovbnya April 19, 2010 at 9:27 am

    Dmitry,
    Have task to automate task of adding users to group.
    I have output file such as:
    Orgname Username
    OP-420-HO-S-00 net1111
    RB-430-AD-A-12 net2222
    AC-200-RL-B-10 net1234
    ….
    etc.
    Orgname=GroupName in AD
    I need to read output file with Posh and add username to appropriate GroupName.
    How i can do this?
    Thanks!

    • 9 Dmitry Sotnikov April 19, 2010 at 9:52 am

      Andriy,

      So the input file is basically space-separated list of groupnames and usernames?

      Then this should work:
      Import-Csv C:\list.csv -Delimiter ‘ ‘ | ForEach-Object {Add-QADGroupMember -Identity $_.Orgname -Member $_.Username }

      If you have further questions, please post them to the forums at http://powergui.org – you might get better answers faster than on this blog because there are more people hanging out there.🙂

      Dmitry

  6. 11 lito May 2, 2012 at 8:30 pm

    Hi Dmitry,

    I attempted the following

    Get-Content c:\Temp\corpdl.csv | Add-QADGroupMember “All Company”

    The CSV file contains a list of email addresses of about 4000 users. The email addresses are either primary SMTP addresses or part of the proxy address. It throws out an error on the third user because the SMTP address is not primary but part of the contact’s proxy addresses. The list contains email addresses that can be either from a contact or user object. Any ideas how I can get this accomplished or what I’m doing wrong?

    Thanks and much appreciated


  1. 1 Dew Drop – December 23, 2009 | Alvin Ashcraft's Morning Dew Trackback on December 23, 2009 at 2:38 pm
  2. 2 Import file to change group membership « Dmitry’s PowerBlog: PowerShell and beyond Trackback on April 19, 2010 at 11:08 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

December 2009
M T W T F S S
« Nov   Jan »
 123456
78910111213
14151617181920
21222324252627
28293031  

%d bloggers like this: