I need a powershell script for exchange that displays all emails fora all the domains?

I have about 12 different domain on my exchange box. how can i get a list and count of all the email addresses, in each domain i have.  organized by domain?
sanehpalAsked:
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.

SubsunCommented:
I guess you are talking about the email domains.. If yes.. Try following code.. Count.csv will have the list of email domain and the recipient count against it... SmtpAddress.csv will have the email list of all recipients.

Run the script from EMS (Exchange Management Shell)
Set-ADServerSettings -ViewEntireForest $true
$Mbxs = Get-Mailbox -ResultSize Unlimited
$Report = @()
Foreach ($Mbx in $Mbxs){
$Mbx.EmailAddresses | ? {$_.PrefixString -eq "smtp"} | %{
	$Report += New-Object PSObject -Property @{
	Name = $Mbx.Name
	SmtpAddress = $_.SmtpAddress
	Domain = ($_.SmtpAddress -split "@")[1]
	}
 }
}
$Report | Group Domain | Select Name,Count | Export-Csv Count.csv -nti
$Report | Select Name,SmtpAddress,Domain | Export-Csv SmtpAddress.csv -nti

Open in new window

PS : The reports include Primary SMTP + the additional email addresses
0
sanehpalAuthor Commented:
That is great and it worked on my local exchange,   I just couldn't get it to work out of office 365.
0
SubsunCommented:
Connect to Exchange Online with Remote PowerShell and run the script..
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sanehpalAuthor Commented:
i am getting a :
Set-ADServerSettings : The term 'Set-ADServerSettings' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At H:\exchangepowershell\test.ps1:1 char:1
+ Set-ADServerSettings -ViewEntireForest $true
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-ADServerSettings:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
SubsunCommented:
Remove first line when you run the script for Exchange Online..
0
sanehpalAuthor Commented:
I did that and the script runs, but the count and smtpaddress files are empty.
0
SubsunCommented:
It should work.. Can you post the result of Get-mailbox command against one user?

Get-mailbox USerA | Select -exp EmailAddresses
0
sanehpalAuthor Commented:
The operation couldn't be performed because object 'USerA' couldn't be found on
'BL2PR04DC003.namprd04.prod.outlook.com'.
    + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=BL2PR04MB163,RequestId=57c92904-9270-4a8e-849e-9dd4da16db66,TimeStamp=6/18/2015
   4:26:31 PM] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 92717063,Microsoft.Exchange.Management.Reci
  pientTasks.GetMailbox
    + PSComputerName        : pod51041psh.outlook.com
0
SubsunCommented:
Did you replace 'USerA' with a user name from your environment?
0
sanehpalAuthor Commented:
yes here is the command:(john@constco.com is the email address and the username)

PS C:\Windows\system32> Get-mailbox john@constco.com | Select -exp john@constco.com
Select : Property "john@constco.com" cannot be found.
At line:1 char:35
+ Get-mailbox john@constco.com | Select -exp john@constco.com
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (John Doe:PSObject) [Select-Object], PSArgumentException
    + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
0
SubsunCommented:
Use..
Get-mailbox john@constco.com | Select -exp EmailAddresses

Open in new window

0
sanehpalAuthor Commented:
this is the result:

PS C:\Windows\system32> Get-mailbox john@constco.com | Select -exp EmailAddresses
Creating a new session for implicit remoting of "Get-Mailbox" command...
SIP:john@constco.com
x500:/o=MicrosoftOnline/ou=External (FYDIBOHF25SPDLT)/cn=Recipients/cn=5a226d4bf85b4530ab74bb9028396192
SMTP:john@constco.com
smtp:john@constco.microsoftonline.com
X500:/o=MicrosoftOnline/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=a843548a-d8bb-4e5d-a23a-7a9
81a652e84
smtp:john@constco.onmicrosoft.com
PS C:\Windows\system32>
0
SubsunCommented:
Hmm.. I am not sure why you are getting the blank file as the command return the expected results..

I have made some modification to code, Try it and see if it gives you the required details...

$Mbxs = Get-Mailbox -ResultSize Unlimited
$Report = @()
Foreach ($Mbx in $Mbxs){
$Mbx.EmailAddresses | ? {$_ -match "smtp"} | %{
	$Report += New-Object PSObject -Property @{
	Name = $Mbx.Name
	SmtpAddress = $_ -replace "smtp:"
	Domain = ($_ -split "@")[1]
	}
 }
}
$Report | Group Domain | Select Name,Count | Export-Csv Count.csv -nti
$Report | Select Name,SmtpAddress,Domain | Export-Csv SmtpAddress.csv -nti

Open in new window

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
sanehpalAuthor Commented:
Awesome!  That worked.  Thanks for your help!
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
Exchange

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.