Avatar of georgedschneider
georgedschneiderFlag for United States of America

asked on 

Get all SMTP addresses for a specific domain

I'm trying to produce an output of all users with a specific primary email address.  The following script will produce this:

Get-Mailbox -ResultSize unlimited|Where-Object {$_.PrimarySMTPAddress.Domain -eq "domain.com"}|Select @{n='Name';e={$_.displayname}},@{n='Primary Email Address';e={$_.primarysmtpaddress}},@{name='All Email Addresses';e={$_.emailaddresses}}

Open in new window


I'd like to exclude all x500 addresses from the output.  I know using Where-Object {($_ -Like "SMTP:*")} will accomplish this but I can't get it to work with specifying desired domain.

Is there a way to look at both primary and secondary domains like i have in this script for primary domain?

Long term I'd like to remove smtp: so the output is just a listing of addresses.
Exchange

Avatar of undefined
Last Comment
Qlemo
Avatar of Qlemo
Qlemo
Flag of Germany image

Your question is unclear.  Do you want a listing of all matching addresses (primary or secondary), without the SMTP prefix?
Avatar of georgedschneider

ASKER

In the output I'd like to have smtp: removed from the email addresses output.  I'd like to have the csv export display each address without this to make it a little easier to work with.  I'm trying to exclude x500 addresses as well.
Avatar of Qlemo
Qlemo
Flag of Germany image

Get-Mailbox -ResultSize unlimited |
  ? {$_.PrimarySMTPAddress.Domain -eq "domain.com"} |
  Select @{n='Name';e={$_.displayname}},
         @{n='Primary Email Address';e={$_.primarysmtpaddress}},
         @{name='All Email Addresses';e={($_.emailaddresses | ? {$_ -like 'SMTP:*'}) -replace 'SMTP:'}}

Open in new window

Avatar of georgedschneider

ASKER

This works great.  I'm trying to incorporate it in the following script but I can't get the logic with the -replace statement to work.

$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "doamin.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = $Mailbox.emailaddresses|? {$_ -like 'SMTP:*'} 
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"PrimaryEmail Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name

Open in new window

Avatar of georgedschneider

ASKER

I thought the following logic was what I needed.  This produces the desired results.


$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "domain.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = ($Mailbox.emailaddresses|? {$_ -like 'SMTP:*'})-replace 'SMTP:'
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"Primary Email Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name

Open in new window

.


However if I try to export to CSV using the following code the All email addresses column does not contain the needed information.  It displays system.object[] instead.

$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "domain.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = ($Mailbox.emailaddresses|? {$_ -like 'SMTP:*'})-replace 'SMTP:'
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"Primary Email Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name|export-csv "C:\test.csv" -notypeinformation

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Exchange
Exchange

Exchange is the server side of a collaborative application product that is part of the Microsoft Server infrastructure. Exchange's major features include email, calendaring, contacts and tasks, support for mobile and web-based access to information, and support for data storage.

213K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo