script to populate ProxyAddresses and Mail attribute based on the UserPrincipalName


I am trying to populate the ProxyAddresses AND mail attribute for ALL users based on their UserPrincipalName.
Need help with the following:

$UPN = Get-ADUser -Filter * |select UserPrincipalName
$user = Get-ADUser -Filter * -Properties mail,ProxyAddresses
$user.ProxyAddresses = ("SMTP:" + $UPN.UserPrincipalName),("SIP:" + $UPN.UserPrincipalName)
$user.mail = $UPN.UserPrincipalName
Set-ADUser -instance $user

It doesn't work, returns me this error:

The property 'ProxyAddresses' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $user.ProxyAddresses = ("SMTP:" + $UPN.UserPrincipalName),("SIP:" + $UPN.UserPri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

What am i missing? Should i use a ForEach Loop?

Thanks in advance,
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.

Do the users currently have anything set for the proxyAddresses attribute?  If so, does it matter if that information is overwritten?

If you can use the Exchange cmdles, it may be better to do so, but I wouldn't be able to provide guidance there.
Not caring about overwriting anything, you could use the following (and I guess you want to set the Mail attribute as well):
Get-ADUser -Filter * -Properties UserPrincipalName | ForEach `
    Set-ADUser $_.samaccountname -EmailAddress "$($_.UserPrincipalName)" -replace @{proxyAddresses = "SMTP:$($_.UserPrincipalName)","SIP:$($_.UserPrincipalName)"}

Open in new window

bmdgiAuthor Commented:
used the following:

Get-ADUser -Filter * -Properties mail,ProxyAddresses | Foreach {
  $_.ProxyAddresses = ("SMTP:" + $_.UserPrincipalName),("SIP:" + $_.UserPrincipalName)
 $_.mail = $_.UserPrincipalName
 Set-ADUser -instance $_

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
It's usual with PowerShell for there to be more than one way to accomplish the same thing.  I know what I posted works, as it is tested.  Glad you found your own method, though I'm not sure why you didn't use my solution (though it does seem you used ideas from my code, in which case an assist is appropriate).
bmdgiAuthor Commented:
found answer
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
Active Directory

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.