Powershell: Modify Exchange 2013 script to work with Office 365

Qlemo wrote this amazing script and I was hoping he (or you) could help me modify it for Office 365.  The email addresses do not output when run against a Office 365 tenant.   Any ideas?

Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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\SMTP_SIP.csv -encoding ascii

Open in new window

LVL 8
K BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
Hi There K B,

The script is fine but you need to get a PS Session to O365 first, so try add this to the beginning of the script:

$OnlineCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $OnlineCred -Authentication Basic -AllowRedirection
Import-PSSession $Session

Open in new window


It will ask for the credential you use to manage the online mailboxes.

Regards

Guy
0
K BAuthor Commented:
Thank you for your reply!
Apologies if I wasn't more clear - I have a separate .PS1 that connects me with no problems.
both 365 and MsolService in one .PS1
I get all the data from the script I just do not get any email addresses (except the field "primarysmtpaddress")
0
Guy LidbetterCommented:
Did this work on 2013?
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

K BAuthor Commented:
Yes it did.
0
Guy LidbetterCommented:
Odd, its not working with my on premise 2013.
I'll have a look at getting the output required, in the interim, before Qlemo catches this and fixes it!
0
K BAuthor Commented:
HA!  He actually said that 365 is not his realm so he will not be participating in this question :-)
Just out of curiosity, what is the error you run into when running it against 2013?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
(... but Qlemo is watching you!)
Maybe there is a flaw in the script, but I cannot detect one. As said, it works with 2013 for us both.
0
Mahesh SharmaCommented:
Try this

Get-Mailbox -resultsize 3 |
  % {
    $usr = Get-User $_.SamAccountName
      
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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 |Where-Object {$_.PrefixString -ceq “smtp” -or "sip"} | ForEach-Object {$_.SmtpAddress}}}
             
  } | Out-File C:\SMTP_SIP.csv -encoding ascii
 ConvertTo-CSV -notype |
 % { $_ -replace '"'} |
 Out-File C:\SMTP_SIP.csv -encoding ascii
0
K BAuthor Commented:
Thank you for your reply Mks!

I received this when I ran your script:

cmdlet ConvertTo-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject:
0
Mahesh SharmaCommented:
My bad, I left an extra line of code.

Get-Mailbox sharmamk |
  % {
    $usr = Get-User $_.SamAccountName
     
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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 |Where-Object {$_.PrefixString -ceq “smtp” -or "sip"} | ForEach-Object {$_.SmtpAddress}}}
             
  } | Export-csv C:\Out.csv -NoTypeinformation
0
K BAuthor Commented:
Thank you.
Same thing Mks... no information in the EmailAddresses field
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Strange things happen, obviously. Could it be that the O365 accounts all have SIP. sip or SNMP prefixes (note the case)? Because my line line 22 should be
                @{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {'snmp', 'sip' -contains $_.PrefixString} | % {$_.SmtpAddress}) -join ';'}}

Open in new window

But the (wrong) condition is from your original code, K B :D.
I've also changed the email address list delimiter to a semi-colon, as discussed in the prior thread.
0
K BAuthor Commented:
I think i know why this isn't working... Thanks to the Alan Byrne...
https://www.cogmotive.com/blog/office-365-tips/export-all-email-addresses-from-office-356

How do we incorporate this into the script:

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv
0
K BAuthor Commented:
Get-Recipient, that is.   with SIP and SMTP addresses represented. :-)
0
K BAuthor Commented:
Better yet, how do I get this one-liner to only provide us with SMTP addresses THEN SIP addresses in the last column(s) of each row ?
As it stands now, it also provides X500:addresses co-mingled with the SIP: and SMTP:and I don't wont those (co-mingled).

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv

Open in new window


like this:
2015-07-14-0040.png
Thank you!
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
What should the exact output be?
You also took notice that there is no prefix?
0
Guy LidbetterCommented:
I didn't receive an error, just no output on the email addresses, well actually just a bunch 'o commas (,,,,,,,,), everything else works fine.

As  a side note, you do not have to use get-recipient for the same output... just modify the last line of the select section that gets the emailaddresses and remove the jiggery pokery... i.e.

@{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {$_.PrefixString -ceq "smtp" -or "sip"} | % {$_.SmtpAddress}) -join ","}}

Open in new window

Becomes
@{$_.EmailAddresses}

Open in new window


That DOES work for me... but gets the x500 as well. So I think on my side there is an issue with that piece of the script formatting.

If you want additional columns for each email address, that will take a bit more work, but I can get that done.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If I understood correctly, you want to have SMTP first, then SIP. What about those addresses without prefix?
And do you want to keep the prefix? (Your original code did not keep it).

I'll assume you want to have list of SMTP and SIP, without prefix, and just in alphabetical order:
@{Name="EmailAddresses"
  Expression={
    ($_.EmailAddresses |
      ? {$_.PrefixString -ne 'X500'} |
      sort AddressString |
      Select -Expand AddressString
    ) -join ';'
}}

Open in new window

And of course my original script only works for SMTP addresses :/, because it extracts SmtpAddress - which exists only if it is SMTP (sounds straight-forward, how could I have missed that???).
0
K BAuthor Commented:
Thank you Qlemo!
I am trying to incorporate your latest update

@{Name="EmailAddresses"
  Expression={
    ($_.EmailAddresses |
      ? {$_.PrefixString -ne 'X500'} |
      sort AddressString |
      Select -Expand AddressString
    ) -join ';'
}}

Open in new window


with this (is this what you envisioned?)

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv

Open in new window


Is there any way you can help me push them together please sir?
K.B.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Guy told that you do not need Get-Reciepient, and I should agree with him. So let's go back to the original code:
Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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 -ne 'X500'} |
                      sort AddressString |
                      Select -Expand AddressString
                    ) -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

0
K BAuthor Commented:
That yielded blank emailaddresses column again.

I noticed I get results with

get-recipient | select emailaddresses
get-mailbox | select emailaddresses

Open in new window


but not

get-user | select emailaddresses

Open in new window


but I am not sure how to incorporate that into the script.
0
K BAuthor Commented:
However Get-Recipient is much more thorough.  Get-Mailbox yields half the rows of data in this tenant as Get-Recipient.
0
Guy LidbetterCommented:
Hi K B, you are right there, the emailaddresses property is not retrieved by the get-user cmdlet.

However, in Qlemo's code, its called by the get-mailbox not the get-user.
0
K BAuthor Commented:
Unfortunately it doesn't yield any data for that column.  It appears Get-Mailbox is not the way to go anyway.  Get-Recipient gets any recipient at all.
I am trying to thin it out so we can get it down to just the emailaddresses policy so maybe we can use this going forward?

Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="EmailAddresses"
                  Expression={
                    ($_.EmailAddresses |
                      ? {$_.PrefixString -ne 'X500'} |
                      sort AddressString |
                      Select -Expand AddressString
                    ) -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

0
K BAuthor Commented:
Okay this gives all the data but without your magic of pulling out the prefixes and not including X500:

Get-Recipient -Resultsize 30 | 
  % {
    $usr = Get-Recipient $_.SamAccountName
    $_ | Select-Object DisplayName, PrimarySmtpAddress, EmailAddresses                
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You should be able to just replace Get-Mailbox with Get-Recipient, leaving anything else as-is.
0
Guy LidbetterCommented:
If you want the original Out-put with just a list of email addresses as you've done in that bit of code, just use this...

Get-recipient -resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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="EmailAddressess";Expression={$_.EmailAddresses}}
  } |  Export-CSV C:\temp\SMTP_SIP.csv -nti -encoding ascii

Open in new window


I can work on the formatting you'd like off line... but will need some effort.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Next test steps:
Get-Recipient -Resultsize 30 | 
  % {
    $_ | Select-Object DisplayName, PrimarySmtpAddress, @{n='EmailAddresses'; e={$_.EmailAddresses}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

Get-Recipient -Resultsize 30 | 
  % {
    $_ | Select-Object DisplayName, PrimarySmtpAddress, @{n='EmailAddresses'; e={$_.EmailAddresses | Select -Expand AddressString}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

0
K BAuthor Commented:
First script gave emailaddresses column filled but without commas in between
Second script no data in emailaddresses column
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I'm flabbergasted. What does
 Get-Recipient -Resultsize 30 | Select -Expand EmailAddresses | gm *addr*, *prefix*
show as object types and properties?
0
K BAuthor Commented:
Here is the information.. no *addr* or *prefix* in there. so here is all
GM.xlsx
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
What the heck? That is a [String], but should be something like ProxyAddress, SMTPAddress or the like. So the type gets lost, and we have no proper means to dissect single addresses but to perform text processing.
0
K BAuthor Commented:
 Get-Recipient -resultsize 3 |select Name, DisplayName, Alias, PrimarySmtpAddress, @{Name="EmailAddresses";Expression={($_.EmailAddresses)}}| Export-Csv hey.csv

Open in new window


this gives SMTP and SIP and X500 with no commas inbetween but on same row.  Progress?
0
Guy LidbetterCommented:
Interesting interesting interesting!!!!

Remember I mentioned this was not working on my 2013 on Premise??

WELL!!! It DOES work... but only when run locally on an exchange server, or a server with Management tools installed. it does NOT work on a called session...

Consider this (run against my own mailbox):

On Exchange Server:
PS C:\Windows\system32> Get-Recipient glidbetter | Select -Expand EmailAddresses | gm *addr*, *prefix*


   TypeName: Microsoft.Exchange.Data.SmtpProxyAddress

Name               MemberType Definition
----               ---------- ----------
AddressString      Property   string AddressString {get;}
IsPrimaryAddress   Property   bool IsPrimaryAddress {get;}
ProxyAddressString Property   string ProxyAddressString {get;}
SmtpAddress        Property   string SmtpAddress {get;}
Prefix             Property   Microsoft.Exchange.Data.ProxyAddressPrefix Prefix {get;}
PrefixString       Property   string PrefixString {get;}


   TypeName: Microsoft.Exchange.Data.X400ProxyAddress

Name               MemberType Definition
----               ---------- ----------
AddressString      Property   string AddressString {get;}
IsPrimaryAddress   Property   bool IsPrimaryAddress {get;}
ProxyAddressString Property   string ProxyAddressString {get;}
Prefix             Property   Microsoft.Exchange.Data.ProxyAddressPrefix Prefix {get;}
PrefixString       Property   string PrefixString {get;}

Open in new window


On my PC with a PSSession to Exchange:
PS C:\Windows\system32>  Get-Recipient glidbetter | Select -Expand EmailAddresses | gm *addr*, *prefix*
PS C:\Windows\system32>

Open in new window

No Result?!?!?!

So I imagine the code is perfect... it has something to do with the way Powershell is managing the sessions, it must be losing something in translation...
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This is indeed related to remote sessions. When importing the session, proxy functions are created, which use marshalled data types - which get deserialized, that is loose all of their special properties.

So, here we go. That should work for the Exchange Shell and in a remote session. I'm stripping off the prefix; if you would like to keep them, remove the -replace 'SMTP:|SIP:'.
Get-Recipient -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

0
K BAuthor Commented:
Amazing!  Thank you!  I am going to open a separate question about exporting the same for Distribution Groups and Contacts.  

One question if I may please.  I modified the script ... (otherwise I got this error often
"The operation couldn't be performed because object 'XYZ WestOffice Distribution Group' couldn't be found on 'XXXXXXDC001.namprd0X.prod.outlook.com'.")
...to filter for only mailuser etc... and I got all the attributes that are included in the script except two:

1. LegacyExchangeDN
2. ForwardingAddress

It seems these attribute require Get-Mailbox.  Is there anyway to include attributes from Get-Mailbox in this same script?  Here is the modified script and I thank you very much again!!

Get-Recipient -resultsize 20 | Where-Object {($_.RecipientType -eq 'UserMailbox') -or ($_.RecipientType -eq 'MailUser') -or ($_.RecipientType -eq 'User')}| 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -Encoding ascii

Open in new window

0
Guy LidbetterCommented:
Done!

Get-Recipient -resultsize 20 | Where-Object {($_.RecipientType -eq 'UserMailbox') -or ($_.RecipientType -eq 'MailUser') -or ($_.RecipientType -eq 'User')}| 
  % {
    $usr = Get-User $_.SamAccountName
	$MailUsr =  Get-Mailbox $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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="LegacyExchangeDN"	;Expression={$MailUsr.legacyExchangeDn}},
				@{Name="ForwardingAddress"	;Expression={$MailUsr.ForwardingAddress}},
                @{Name="EmailAddresses"
                  Expression={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } 

Open in new window

0
K BAuthor Commented:
Thank for the reply Guy!
That seems to give the error:

"The operation couldn't be performed because object 'XYZ 012012' couldn't be found on 'XXXXXXDC001.namprd0X.prod.outlook.com'."

It also seems to shift data to other columns and leaves some blank rows. Any idea why?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Are you certain you really need Get-Recipient instead of Get-Mailbox?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
K BAuthor Commented:
hmm let me give it a try
0
Guy LidbetterCommented:
Keep in mind, the difference I mentioned earlier. Get-mailbox will return only mailbox USERS. Get-recipient will return distribution groups, rooms, contacts... Anything with an email.

Get-mailbox also returns a lot more information on the objects as well.

Also, the reason you're getting the error is its trying to get mailbox on something get-recipient is returning. It may be trying to get a mailbox for a distribution or similar which it can't return.
0
K BAuthor Commented:
Final script

Get-Mailbox -resultsize 30 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress, ForwardingAddress, LegacyExchangeDN,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{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={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -Encoding ascii

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.