1

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.

SeanP
  • 13
  • 2

1 Answers1

0

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"
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    Wow! thanks a lot Santiago! I had to read it a few time to understand but that works awesome! – SeanP Jan 27 '22 at 22:40