script to populate ProxyAddresses and Mail attribute based on the UserPrincipalName

Posted on 2014-08-12
Last Modified: 2014-08-23

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,
Question by:bmdgi
    LVL 38

    Expert Comment

    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.
    LVL 38

    Expert Comment

    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

    LVL 1

    Accepted Solution

    used the following:

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

    Expert Comment

    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).
    LVL 1

    Author Closing Comment

    found answer

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
    I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now