Script to change UPN in OU

#Script to update the UPN suffix
#Replace the fields indicated with <> with actual field names
import-module ActiveDirectory
Get-ADUser -SearchBase "OU=Orange Domain Users,OU=Faculty,DC=mail,DC=orange,DC=k12,DC=nj,DC=us" -SearchScope OneLevel -filter * |
ForEach-Object {
$newUPN = $_.UserPrincipalName.Replace('mail.orange.k12.nj.us', 'orange.k12.nj.us')
$_ | Set-ADUser -server OHSAPPS -UserPrincipalName $newUPN
}

When I run the script from Windows 7 machine I get the error:

Get-ADUser : Directory object not found
At C:\Scripts\UPNORANGE.PS1:4 char:11
+ Get-ADUser <<<<  -SearchBase "OU=Orange Domain Users,OU=Faculty,DC=mail,DC=orange,DC=k12,DC=nj,DC=us" -SearchScope OneLevel -filter * |
    + CategoryInfo          : ObjectNotFound: (:) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDirectory.Management.Commands.GetADUser

April 21st, 2015 11:41am

Error message is that the directory object was not found. Either the OU in your SearchBase is incorrect, or there are no user objects located in that OU, since you have SearchScope set to OneLevel, it will only look in that OU and one level deep, so make sure your command is setup correctly for what you are trying to target.
Free Windows Admin Tool Kit Click here and download it now
April 21st, 2015 11:52am

The OU was incorrect:OU=Faculty,OU=Orange Domain Users,DC=Mail,DC=orange,DC=k12,DC=nj,DC=us

Now I am getting different error:

#Script to update the UPN suffix
#Replace the fields indicated with <> with actual field names
import-module ActiveDirectory
Get-ADUser -SearchBase "OU=Faculty,OU=Orange Domain Users,DC=Mail,DC=orange,DC=k12,DC=nj,DC=us" -SearchScope OneLevel -filter * |
ForEach-Object {
$newUPN = $_.UserPrincipalName.Replace('mail.orange.k12.nj.us', 'orange.k12.nj.us')
$_ | Set-ADUser -server OHSAPPS -UserPrincipalName $newUPN
}

You cannot call a method on a null-valued expression.
At C:\Scripts\UPNORANGE.PS1:6 char:39
+ $newUPN = $_.UserPrincipalName.Replace <<<< ('mail.orange.k12.nj.us', 'orange.k12.nj.us')
    + CategoryInfo          : InvalidOperation: (Replace:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

April 21st, 2015 12:05pm

Now I run the following scripts to change the UPN , the script runs howver no changes are made

#Script to update the UPN suffix
#Replace the fields indicated with <> with actual field names
import-module ActiveDirectory
Get-ADUser -SearchBase "OU=Faculty,OU=Orange Domain Users,DC=Mail,DC=orange,DC=k12,DC=nj,DC=us" -SearchScope OneLevel -filter * |
ForEach-Object {
$newUPN = $_.UserPrincipalName.Replace('mail.orange.k12.nj.us', 'orange.k12.nj.us')
$_ | Set-ADUser -server OHSAPPS -UserPrincipalName $newUPN
}

Free Windows Admin Tool Kit Click here and download it now
April 21st, 2015 12:37pm

You can try the -Replace param instead of using the UserPrincipalName param

Set-ADUser -server OHSAPPS -Replace @{UserPrincipalName="$newUPN"}

April 21st, 2015 1:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics