Exchange 2010 powershell store primary smtpaddress in a variable

I am trying to write a simple script however I am coming across a strange problem. I would like to store the primarysmtpaddress value in a variable. The code I am using is below which i thought would be very simple but it isnt working as i expected.

$user = get-mailbox hutchl
$mainemail = $user.primarysmtpaddress

Open in new window


Running the command get-mailbox hutchl | select-object primarysmtpaddress returns the results as I expect.
select-object
However running the script above does not. It returns multiple values when stored in a variable.
stored in variable

All I am trying to do is store the primarysmtpaddress into a variable of my choosing.
LVL 35
Joseph DalyAsked:
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:
Primarysmtpaddress is a multivalued property.. to get the Primarysmtpaddress as single value you need to convert it to string..
Try..
$User.Primarysmtpaddress.ToString()
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
Joseph DalyAuthor Commented:
Looks like adding the .tostring worked.
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.