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

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
LVL 1
Thor2923Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jason CrawfordTransport NinjaCommented:
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.
1
Thor2923Author 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?
0
MichelangeloConsultantCommented:
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

0
Make Network Traffic Fast and Furious with SD-WAN

Software-defined WAN (SD-WAN) is a technology that determines the most effective way to route traffic to and from datacenter sites. Register for the webinar today to learn how your business can benefit from SD-WAN!

MichelangeloConsultantCommented:
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
1

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
Thor2923Author 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?
0
yo_beeDirector of Information TechnologyCommented:
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

1
MichelangeloConsultantCommented:
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

0
Thor2923Author 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
0
yo_beeDirector of Information TechnologyCommented:
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.
0
Jason CrawfordTransport NinjaCommented:
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?
0
yo_beeDirector of Information TechnologyCommented:
@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.
0
Jason CrawfordTransport NinjaCommented:
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'.
1
yo_beeDirector of Information TechnologyCommented:
There a so many ways to skin this cat.  I just gave the Asker what he was asking for.
1
Sara TeasdaleCommented:
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

1
Thor2923Author Commented:
lots of good input thanks
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
Powershell

From novice to tech pro — start learning today.