Solved

Exchange PowerShell:  Get all Primary and Alias SMTP Addresses of members of Distribution List

Posted on 2013-06-24
10
703 Views
Last Modified: 2013-07-24
I have a Distribution List and need the following information from the members of this distribution list:

AliasSMTPAddress PrimarySMTPAddress
AliasSMTPAddress PrimarySMTPAddress
AliasSMTPAddress PrimarySMTPAddress
AliasSMTPAddress PrimarySMTPAddress

e.g.
Frank@gmail.com Frank.Henry@gmail.com
Frank.h@gmail.com Frank.Henry@gmail.com
F.Henry@gmail.com Frank.Henry@gmail.com
Lara@gmail.com Lara.Chandler@gmail.com
Lara.C@gmail.com Lara.Chandler@gmail.com
L.Chandler@gmail.com Lara.Chandler@gmail.com

Open in new window

0
Comment
Question by:whenz
  • 6
  • 4
10 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Try this and see if it works as expected..
Get-DistributionGroupMember "GroupName" | Get-mailbox | % {
for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
{
 if ($_.EmailAddresses[$i].ProxyAddressString  -like “smtp:*” `
	-and $_.EmailAddresses[$i].ProxyAddressString -ne "SMTP:$($_.PrimarySmtpAddress.tostring())")
  {
  Add-Content C:\Group.txt "$($_.EmailAddresses[$i].ProxyAddressString) $($_.PrimarySmtpAddress.ToString())"
  }
 }
}

Open in new window

0
 

Author Comment

by:whenz
Comment Utility
Hi Subsun

Sorry for my late reply.  The output looks fine with following expections:

1) The lines always begin with smtp:  Would it be possible to get an output without smtp: ?  

2) The script only works if user mailboxes are members of the DL.  But I also have DL's, contacts, public folders which are members of the DL.  Would it be possible to have a script which exports the aliases of these objects as well?
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Get-DistributionGroupMember "GroupName"  | Get-Recipient | % {
for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
{
 if ($_.EmailAddresses[$i].ProxyAddressString  -like “smtp:*” `
	-and $_.EmailAddresses[$i].ProxyAddressString -ne "SMTP:$($_.PrimarySmtpAddress.tostring())")
  {
  Add-Content C:\Group.txt "$($_.EmailAddresses[$i].ProxyAddressString -Replace "^smtp:") $($_.PrimarySmtpAddress.ToString())"
  }
 }
}

Open in new window

Keep in mind the script will output the recipient which has a different alias address. If a recipient have only primary SMTP address then it wont display in the output (I presume that's what you looking for)
0
 

Author Comment

by:whenz
Comment Utility
Thank you Subsun, your script works perfectly fine except that it does not read the aliases from public folders.  would that be possible?
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
It should read aliases from all recipients including public folders. As I mentioned above If a recipient have only primary SMTP address then it wont display in the output. Does your public folder have more than one email address?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:whenz
Comment Utility
Hello Subsun,

You're right, was my mistake.  Our public folders only have 1 email address, the primary SMTP address.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
So are we good? or do you want to add those addresses in report?
0
 

Author Comment

by:whenz
Comment Utility
I would like to add the primary smtp addresses of public folders to the report.  that would be awesome!
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Check this code and see if the output format is as expected..
Get-DistributionGroupMember "GroupName"  | Get-Recipient | %{
$Primary = $_.PrimarySmtpAddress.ToString()

$_.EmailAddresses | ? {$_.SmtpAddress} | Select -Expandproperty SmtpAddress | % {
	If ($_ -ne $Primary) {
		Add-Content C:\Group.txt "$_ $Primary"
		}
	Else{
		Add-Content C:\Group.txt "$Primary"
		}
	}
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
Or Try this.. Following code will display the email addresses in following format..
If there is only one email address for the recipient then it will write just PrimarySMTPAddress to the file else it will write..
AliasSMTPAddress PrimarySMTPAddress

Get-DistributionGroupMember "GroupName"  | Get-Recipient | %{
$Primary = $_.PrimarySmtpAddress.ToString()
$Email = $_.EmailAddresses | ? {$_.SmtpAddress}
	If (($Email | Measure).Count -gt 1) {
	 $Email | Select -Expandproperty SmtpAddress | % {
		If ($_ -ne $Primary) {
			Add-Content C:\Group.txt "$_ $Primary"
			}
		}
	}
	Else{
		Add-Content C:\Group.txt "$Primary"
	}
}

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now