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.
Select-Object : Cannot process argument because the value of argument “obj” is
null. Change the value of argument “obj” to a non-null value.
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
I encounter the same error as Anon
Get-QADGroupMember MyGroupName -Type user -Indirect
Output returns a few hundred rows.
Any other thoughts?
Pete
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
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
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
That was indeed the problem;), thank YOU…Pete
Cool. Thanks for confirming it!