Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Script to swap Proxyaddresses and PrincipalName

Been writing script:

foreach ($Username in (Import-Csv -Path "C:\Users\...\nope.csv")) {

    set-aduser $Username.Username -remove @{Proxyaddresses="smtp:$Username.Username@domain.com"}
        Write-Output "remove proxy"
    set-aduser $Username.Username -replace @{userPrincipalName="$Username.Username@domain.com"}
        Write-Output "replace principal"
    set-aduser $Username.Username -add @{Proxyaddresses="smtp:$Username.Username@domain.com"}
        Write-Output "add proxy"

}

the code runs but ends up putting this garbage -> @{Username=###}.Username@domain.com
in the attribute Editor.

Any help would be appreciated, been trying different ways for like 8h now.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Basically the double-quotes are expanding the object $UserName however are not allowing you to reference the property UserName of your object. An easy way to get around this is to use the Subexpression operator $( ):

$object = [pscustomobject]@{
    Hello = 'World!'
}

$object.Hello      # => World!
"$object.Hello"    # => @{Hello=World!}.Hello
"$($object.Hello)" # => World!

On the other hand, a much easier solution in this case would be to reference the UserName property while enumerating the collection:

foreach ($UserName in (Import-Csv -Path "C:\Users\...\nope.csv").UserName) {
    $upn = "$UserName@newDomain.com"
    Set-ADUser $UserName -Remove @{ ProxyAddresses = "smtp:$UserName@currentDomain.com" }
    Write-Output "Remove ProxyAddresses for $UserName"
    Set-ADUser $UserName -Replace @{ userPrincipalName = $upn }
    Write-Output "Replace UserPrincipalName for $UserName"
    Set-ADUser $UserName -Add @{ ProxyAddresses = "smtp:$upn" }
    Write-Output "Add ProxyAddresses for $UserName"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading