I need to export a list of exchange users with their email addresses.

Thor2923
Thor2923 used Ask the Experts™
on
I need a powershell script that will export all Exchange users with their display name and email addresses. If more info is included that is fine, but I am hoping to give our HR department a spread sheet with a couple thousand names on it that looks something like

Arthur Anderson AAnderson@domain.com
Becky Blue           Bblue@domain.com
Cathy Clue            CClue@domain.com


and so on.....I am hoping to just paste a command line in and have it generate a csv file on the local c drive in a place such as C:\temp
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Get-Mailbox -ResultSize unlimited | Select-Object displayname,primarysmtpaddress | Export-Csv "c:\temp\ExchangeUsers.csv" -NoTypeInformation

Open in new window

Pretty easy one-liner.  Let me know if it needs to be adjusted at all.

Author

Commented:
That is exactly what I asked for! BUT...in looking at it I see former employees on the list. Is there a way to list ONLY the employees that are enabled?
MichelangeloSystem Administrator / Postmaster

Commented:
In case your users have more than one email address defined on their mailbox you can extract all of them. All data is saved on one line with $delim as delimiter. Delimiter and file extension can be changed.
example output:
-----------------------
user1;user1@domain1.com;user1@domain2.com
user2;user2@domain1.com

$delim = ";"
get-mailbox -resultsize unlimited | % {$_.displayname + "$delim" + ($_.emailaddresses.smtpaddress -join "$delim") } > $env:userprofile\addressreport.csv
write-host "report saved as $env:userprofile\addressreport.csv"

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

System Administrator / Postmaster
Commented:
You can filter get-mailbox  results but you have to define a criteria.
get-mailbox returns all users with a mailbox. Should you mean all mailbox with the flag IsMailboxEnabled set to $true, you can get al of them with
Get-Mailbox -resultsize unlimited –Filter {(IsMailboxEnabled -eq $true)}

Open in new window

then pipe the results to the rest of the script

Author

Commented:
well that just produced a lot of names with no addresses. I do not care about multiple addresses all I need is the users names with the reply address. the first command line was great but includes more than I need. Is there a way to trim the disabled users?
yo_beeDirector of Information Technology

Commented:
You will need to use activeDirectory Module in conjunction with exchange.

You need both EMC and Active Directory Powershell  installed
Tweaked a bit from the above recommendations

Get-PSSnapin -Registered | Add-PSSnapin
Import-module ActiveDirectory

$ActiveUsers = Get-ADUser -filter * -properties enabled | ?{$_.enabled -eq $true}
$A = @()
Foreach ($mailbox in $ActiveUsers)
{
$A += Get-Mailbox -Identity $mailbox.SamAccountName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue| select displayname,primarysmtpaddress 
$A | Export-Csv -Path C:\temp\test.csv -NoTypeInformation -Force 
}

Open in new window

MichelangeloSystem Administrator / Postmaster

Commented:
It's not clear to me what produced lot of names with no addresses, if you paste the command i may be able to help.
We can filter out disabled users  but we have to know the criteria which makes them disabled. The following filters out disabled users in AD:

Get-Mailbox -resultsize unlimited | ? { (Get-User $_.Alias).UserAccountControl -notmatch 'AccountDisabled' } | select-object DisplayName,PrimarySMTPaddress | Export-Csv "c:\temp\ExchangeUsers.csv" -NoTypeInformation

Open in new window

Author

Commented:
well Yo_bee that appeared to run pretty cool but I ended up with >> at the end and no output in my C:\temp folder so I assume something went wrong
yo_beeDirector of Information Technology

Commented:
can you post your code?
I would also run it without the | Export-Csv -Path C:\temp\test.csv -NoTypeInformation -Force
Get-PSSnapin -Registered | Add-PSSnapin
Import-module ActiveDirectory

$ActiveUsers = Get-ADUser -filter * -properties enabled | ?{$_.enabled -eq $true}
$A = @()
Foreach ($mailbox in $ActiveUsers)
{
$A += Get-Mailbox -Identity $mailbox.SamAccountName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue| select displayname,primarysmtpaddress 
$A 
}

Open in new window


to verify if $a has an output.

Commented:
Wait so you leave mailboxes for former employees enabled?  Everyone is assuming you disable the AD user, can you confirm?  Why do  you leave the mailboxes enabled?
yo_beeDirector of Information Technology

Commented:
@jason

In my environment we monitor  their mailbox for a period of time, but there is no reason to leave them enabled. This closes a possible security issue if we left their accountable.

Commented:
Ok to each his/her own.  I generally just add the email address as a proxy to whoever wants to monitor the mail flow and disable the mailbox but either works.  To get around the present snag you'd have to mark the mailbox somehow either by assigning a value to a CustomAttribute (like F for FIRED or get the F out of here haha) or some other filterable property.  You could compare disabled users to enabled mailboxes, but that seems prone to accidental mistakes.  Let me know what you decide and I can rewrite my version of the 'script'.
yo_beeDirector of Information Technology

Commented:
There a so many ways to skin this cat.  I just gave the Asker what he was asking for.
Export list of all email addresses to a .CSV file using Exchange Management Shell (EMS):
Get-Mailbox -ResultSize Unlimited -OrganizationalUnit “OU=Phoenix Users,OU=Phoenix,DC=CONTOSO,DC=COM” |Select-Object DisplayName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}} | Export-CSV c:\exportsmtp.csv -NoTypeInformation

Open in new window

Author

Commented:
lots of good input thanks

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