Link to home
Start Free TrialLog in
Avatar of Thor2923
Thor2923Flag for United States of America

asked on

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
SOLUTION
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thor2923

ASKER

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?
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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

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

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
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.
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?
@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.
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'.
There a so many ways to skin this cat.  I just gave the Asker what he was asking for.
Avatar of Sara Teasdale
Sara Teasdale

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

lots of good input thanks