Extract all email addresses with PowerShell from Exchange

K B
K B used Ask the Experts™
on
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="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{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?

K.B.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think it's your -or try:

? {$_.PrefixString -ceq "smtp" -or $_.PrefixString -ceq "sip"}

Or:

? {$_.PrefixString -cmatch "(^smtp$|^sip$)"}

Also you can use the IsPrimaryAddress property

? {!$_.IsPrimaryAddress}
K B

Author

Commented:
Thank you!!!! worked great!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial