locked
Dump of Primary/Secondary E-mail Addresses RRS feed

  • Question

  • I'm looking for a PowerShell command to dump all of the Primary and secondary addresses in our organization. I found this command, which works, but I need to dump in a different format.

    Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,ServerName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}}

    The format I need output would be like this, instead of having all of the secondary's on one line like the output from the above command.

    PrimaryAddress1@domain.com                 SecondaryAddress1@domain.com
    PrimaryAddress1@domain.com                 SecondaryAddress2@domain.com

    Tuesday, December 10, 2013 3:17 PM

Answers

  • How about something like this?

    Get-Mailbox -ResultSize Unlimited | ForEach {
      $d = $_.DisplayName
      $s = $_.ServerName
      $p = $_.PrimarySmtpAddress
      foreach ($x in $_.EmailAddresses) {
        if ($x.PrefixString -ceq 'smtp')
        {
          $d + "`t" + $s + "`t" + $p + "`t" + $x.SmtpAddress
        }
      }
    }


    --- Rich Matheisen MCSE&I, Exchange MVP

    • Marked as answer by agroda Wednesday, December 11, 2013 10:10 PM
    Wednesday, December 11, 2013 3:39 AM

All replies

  • How about something like this?

    Get-Mailbox -ResultSize Unlimited | ForEach {
      $d = $_.DisplayName
      $s = $_.ServerName
      $p = $_.PrimarySmtpAddress
      foreach ($x in $_.EmailAddresses) {
        if ($x.PrefixString -ceq 'smtp')
        {
          $d + "`t" + $s + "`t" + $p + "`t" + $x.SmtpAddress
        }
      }
    }


    --- Rich Matheisen MCSE&I, Exchange MVP

    • Marked as answer by agroda Wednesday, December 11, 2013 10:10 PM
    Wednesday, December 11, 2013 3:39 AM
  • This looks like it's producing the correct output.  However, now I need to dump it to a CSV file and I'm having a hard time.  I simply added a "| Export-CSV C:\smtp.csv -NoTypeInformation" after the last bracket, but it's just dumping a bunch of numbers.


    EDIT:  Nevermind, I just used the DOS method of " > file.csv" instead of trying to make "Export-CSV" work.  Thanks for the solution!!
    • Edited by agroda Wednesday, December 11, 2013 10:10 PM
    Wednesday, December 11, 2013 4:24 PM
  • Redirection is one way to do that. Another is to make two simple modifications.

    At the top of the script, add this line to write a header row:

    "DisplayName`tServer`tPrimaryAddress`tSecondaryAddress" | out-file <name>

    Then change the line that displays the information so it appends to the same file:

    $d + "`t" + $s + "`t" + $p + "`t" + $x.SmtpAddress | out-file <name> -append

    That'll give you a tab-separated values file you can use.


    --- Rich Matheisen MCSE&I, Exchange MVP

    Thursday, December 12, 2013 2:40 AM