jshizzle
jshizzle

Reputation: 487

PowerShell Get-ADuser value as string

Part of a script that I'm working on grabs users from AD passes to the variable $TargetUsers

This variable is then passed on to the following to change the UPN suffix for each user:

$OldSuffix = "@olddomain.com"
$NewSuffix = "@newdomain.com"
Foreach ($User3 in $TargetUsers) {
$Upn3 = $User3.UserPrincipalName
$NewUpn = $User3.UserPrincipalName.Replace($OldSuffix,$NewSuffix)
    Get-ADUser -Filter "UserPrincipalName -eq '$Upn3'" | Set-ADuser `
        -Remove @{proxyAddresses=@("SMTP:$($User3.givenName).$($User3.sn)$OldSuffix","sip:$($User3.givenName).$($User3.sn)$OldSuffix")} `
        -Add @{proxyAddresses=@("SMTP:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix","smtp:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$OldSuffix","sip:$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix")} `
        -Replace @{ co = "United Kingdom" } `
        -Email "$($User3.givenName.ToLower()).$($User3.sn.ToLower())$NewSuffix" `
        -UserPrincipalName $NewUpn
    $NewUpn2 = Get-ADuser -Filter "UserPrincipalName -eq '$NewUpn'" | Select UserPrincipalName
    "$(Get-Date -f HH:mm:ss):  $($Upn3): AD Attributes updated & UPN Suffix changed to $NewUpn2" | Tee-Object $UserMigrationLog -Append
    }

This outputs to host and log file "...UPN Suffix changed to @{[email protected]}"

How can I get this to exclude the "@{UserPrincipalName=" & trailing "}" at the end?

Upvotes: 1

Views: 7502

Answers (1)

jshizzle
jshizzle

Reputation: 487

Ah sorted it.

$NewUpn2 = Get-ADuser -Filter "UserPrincipalName -eq '$NewUpn'" | % {$_.UserPrincipalName}

Did the trick.

Upvotes: 1

Related Questions