Get a list of users’ email addresses

Here’s a one-liner to turn members of a group into a list of email addresses, separated by semicolon. I am using it every now and then when someone from our partners (which obviously do not have access to our address book) ask me for a list of folks to include in some discussions, or grant access to some resources, and so on.

Here’s the oneliner (for PowerShell v2):

(Get-QADGroupMember MyGroupName -Type user -Indirect |
    Select -expand Email) -join ';'

PowerShell v1 version has a slightly different syntax for join:

[string]::join(';',
  (Get-QADGroupMember MyGroupName -Type user -Indirect |
    Select -expand Email))

And here’s a quick explanation of what it does:

  • I use Get-QADGroupMember to retrieve all members of the group. Note that -Indirect parameter gives me all members of nested groups, and -Type user makes sure that nested groups themselves get excluded.
  • Then I am taking the collection of user objects and turn that into a collection of just one property of the objects (Email) using  Select -expand.
  • Finally I am using join to turn that collection into a string and using semicolon as separator.

Hope this is useful.

8 Responses to “Get a list of users’ email addresses”


  1. 1 Anon November 7, 2011 at 3:49 pm

    Select-Object : Cannot process argument because the value of argument “obj” is
    null. Change the value of argument “obj” to a non-null value.

    • 2 Dmitry Sotnikov November 7, 2011 at 6:05 pm

      Anon,

      Are you sure the first cmdlet actually returned anything? Maybe it indeed returned nothing (null). Try just running:

      Get-QADGroupMember MyGroupName -Type user -Indirect

      And see if you get any output.

      Dmitry

  2. 3 Pete January 9, 2012 at 12:35 am

    I encounter the same error as Anon

    Get-QADGroupMember MyGroupName -Type user -Indirect

    Output returns a few hundred rows.

    Any other thoughts?
    Pete

    • 4 Dmitry Sotnikov January 9, 2012 at 6:35 pm

      Pete,

      You mean you are only getting a few hundred members but you are absolutely sure that this group has more users?

      Try posting the repro steps to our AD and PowerShell forum here and we will try to figure out the problem:

      http://powergui.org/forumindex.jspa?categoryID=55

      Dmitry

      • 5 Pete January 9, 2012 at 10:41 pm

        Hi Dmitry,

        To clarify, I mean the one liner:
        (Get-QADGroupMember MyGroupName -Type user -Indirect |
        Select -expand Email) -join ‘;’
        Results in same error as Anon: –
        Select-Object : Cannot process argument because the value of argument “obj” is null. Change the value of argument “obj” to a non-null value.

        But when test return using your suggested Get-QADGroupMember MyGroupName -Type user -Indirect it’s returns hundreds of rows.

        Will post details as suggested;)
        Cheers! Pete

      • 6 Dmitry Sotnikov January 12, 2012 at 11:20 pm

        Pete,

        I think I know what the issue is – your group probably includes user accounts that are not mail-enabled, and this makes ‘Select -Expand Email’ fail.

        Try using LDAP filter to remove those:

        (Get-QADGroupMember QDL.BU.CloudComputing.Discussion -ldapfilter ‘(&(objectClass=user)(mail=*))’ -Indirect | Select -expand Email) -join ‘;’

        Dmitry

  3. 7 Pete January 13, 2012 at 12:34 am

    That was indeed the problem;), thank YOU…Pete


Leave a comment




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

October 2009
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031