PowerShell – Query AD for Group Email Addresses…

Somebody asked me today if there is an easy way to query AD or Exchange for the email addresses of groups.  There most certainly is!  Let’s walk through my train-of-thought here.  First, I started by querying all groups, piped to Format-List, like this…

Get-ADObject -LDAPFilter "(objectclass=group)" -Properties mail|fl

…but in doing so, you get this list of group objects that may or may not have email addresses:

PowerShell Group Email Query

Hmm, we can do better…  Do you want to query ALL groups, or just groups with email addresses?  Well, in our case, we’re really just after groups with email addresses, so I modify my ldap filter accordingly:

Get-ADObject -LDAPFilter "(&(objectclass=group)(mail=*))" -Properties mail|fl

And the results look the same as above, but only the groups we want are listed.  Okay, that’s better.  But of course, if you ONLY want the email addresses, let’s limit the properties in our list:

Get-ADObject -LDAPFilter "(&(objectclass=group)(mail=*))" -Properties mail|fl -Property mail

…and it looks like this:

PowerShell Group Email Query 2

And finally, if you just want the list of email addresses, no attributes; we use the PowerShell equivalent of AWK to split the line…

Get-ADObject -LDAPFilter "(&(objectclass=group)(mail=*))" -Properties mail|fl -Property mail|findstr /v "^$"| %{ $_.Split(' ')[2]; }

And here’s our pretty list:

PowerShell Group Email Query 3

 I hope that helps!

2014-09-10T22:11:03+00:00 September 10th, 2014|Uncategorized|

About the Author:

Jeremy is just a regular guy that likes to occasionally tell the world about stuff.

Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://dc.services.visualstudio.com/v2/track` resulted in a `400 Invalid instrumentation key` response: {"itemsReceived":1,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"}]} ' in /home/coretek/public_html/wp-content/plugins/application-insights/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/coretek/public_html/wp-content/plugins/application-insights/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /home/coretek/public_html/wp-content/plugins/application-insights/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /home/coretek/public_html/wp-content/plugins/application-insights/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promi in /home/coretek/public_html/wp-content/plugins/application-insights/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113