We help IT Professionals succeed at work.
Get Started

Extract all email addresses with PowerShell from Exchange

Last Modified: 2016-03-07
I cannot seem to figure out why the portion of the code that reads...
? {$_.PrefixString -ceq "smtp" -or "sip"}

Open in new window

...from the below script is not filtering out primary SMTP addresses.

So, I do not want the primary SMTP address to appear in the output at all.

Get-Mailbox -ResultSize Unlimited| 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={($usr.StreetAddress) -replace '(.*?)[\r|\n](.*?)' , '$1 $2' }},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {$_.PrefixString -ceq "smtp" -or "sip"} | % {$_.SmtpAddress}) -join ";"}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\Mailboxes.csv

Open in new window

Any ideas experts?

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE