Using powershell and get-mailbox command to display raw smtp addresses

Hi, I'm trying to pass the windowsemailaddress field from the get-mailbox command using the Exchange 2007 Powershell to another part of a script.

Unfortunately, when I use something like:

Get-Mailbox | select WindowsEmailAddress
or
Get-Mailbox | fl WindowsEmailAddress

it passes the column heading and some other weird characters to the variable. The actual code I'm using is included.

How can I pass raw email addresses to the next part of the script? ie. like they were from a text file and each smtp address was on a new line, no spaces or carrage returns etc.

Thanks
[void][Reflection.Assembly]::LoadFile("E:\Exchange scripts\ewsutil\EWSUtil.dll")
$AllAddresses = Get-Mailbox | select WindowsEmailAddress
foreach($userEM in $AllAddresses)
{
$setPPTo = "0"
$casUrl = "https://fsatg10ech001/ews/exchange.asmx"
$mbMailboxEmail= $userEM
$ewc = new-object EWSUtil.EWSConnection("migrator2@# removed by modernmatt #",$true, $null, $null, $null,$casUrl)
 
$fldarry = new-object EWSUtil.EWS.BaseFolderIdType[] 1
$dTypeFld = new-object EWSUtil.EWS.DistinguishedFolderIdType
$dTypeFld.Id = [EWSUtil.EWS.DistinguishedFolderIdNameType]::msgfolderroot
$mbMailbox = new-object EWSUtil.EWS.EmailAddressType
$mbMailbox.EmailAddress = $mbMailboxEmail
$dTypeFld.Mailbox = $mbMailbox
$fldarry[0] = $dTypeFld
 
write $mbMailbox

Open in new window

Al230762Asked:
Who is Participating?
 
peter_fieldCommented:
try this:
$AllAddresses = Get-Mailbox | %{ $_.WindowsEmailAddress.ToString() }

Open in new window

0
 
Al230762Author Commented:
Dude that's awesome, thanks.
0
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.

All Courses

From novice to tech pro — start learning today.