SOLVED

Changing UPN with Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-1747062%22%20slang%3D%22en-US%22%3EChanging%20UPN%20with%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1747062%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20change%20the%20UPN%20on%20about%20200%20accounts%20in%20our%20company.%20I%20have%20a%20CSV%20file%20with%20the%20old%20and%20new%20UPNs%20in%20question.%20The%20script%20I%20am%20trying%20to%20use%20is%20here%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FUpdate-UPNs-from-CSV-0acbd718%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FUpdate-UPNs-from-CSV-0acbd718%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%26lt%3B%23%20%0A%20.NOTES%0A%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%20%20Created%20with%3A%20%20SAPIEN%20Technologies%2C%20Inc.%2C%20PowerShell%20Studio%202016%20v5.2.128%0A%20%20Created%20on%3A%20%20%20%2013%2F11%2F2016%2015%3A04%0A%20%20Created%20by%3A%20%20%20%20Maurice%20Daly%20%0A%20%20Filename%3A%20%20%20%20%20%20UpdateUPN.ps1%0A%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%20.DESCRIPTION%0A%20%20Reads%20the%20contents%20of%20a%20CSV%20specified%20during%20the%20runtime%20command%2C%0A%20%20then%20updates%20all%20users%20contained%20with%20their%20new%20UPN.%0A%20%20within.%0A%20.EXAMPLE%0A%20%20UpdateUPN.ps1%20-SourceFile%20%22C%3A%5CUsers.CSV%22%0A%20%20%0A%20%20Source%20CSV%20must%20contain%20headings%20OLDUPN%20and%20NEWUPN%20to%20function%0A%0A%20Use%20%3A%20This%20script%20is%20provided%20as%20it%20and%20I%20accept%20no%20responsibility%20for%0A%20any%20issues%20arising%20from%20its%20use.%0A%0A%20Twitter%20%3A%20%40modaly_it%0A%20Blog%20%3A%20https%3A%2F%2Fmodalyitblog.com%2F%0A%23%26gt%3B%0A%5BCmdletBinding(SupportsShouldProcess%20%3D%20%24true)%5D%0Aparam%20(%0A%20%5Bparameter(Mandatory%20%3D%20%24true%2C%20HelpMessage%20%3D%20%22Location%20of%20CSV%20file%20containing%20both%20old%20and%20new%20UPNs%22%2C%20Position%20%3D%201)%5D%0A%20%5BValidateNotNullOrEmpty()%5D%0A%20%5BValidateScript(%7B%20Test-Path%20%24_%20%7D)%5D%0A%20%5Bstring%5D%24SourceFile%0A)%0A%0A%23%20Import%20UPN%20detals%0A%24UPNDetails%20%3D%20Import-Csv%20-Path%20%24SourceFile%0A%0A%23%20Import%20Active%20Directory%20commandlets%0AImport-Module%20-Name%20ActiveDirectory%0A%0A%23%20Loop%20for%20each%20user%0Aforeach%20(%24User%20in%20%24UPNDetails)%0A%7B%0A%20%23%20Get%20AD%20user%20details%20using%20old%20UPN%20and%20the%20update%0A%20%24UserDetails%20%3D%20Get-ADUser%20-filter%20*%20%7C%20Where-Object%20%7B%20%24_.UserPrincipalName%20-eq%20%24User.OldUPN%20%7D%0A%20Write-Output%20%22Changing%20UPN%20%24(%24User.OldUPN)%20to%20%24(%24User.NewUPN)%22%0A%20Set-ADUser%20-Identity%20%24UserDetails.Name%20-UserPrincipalName%20(%24UserDetails.UserPrincipalName%20-replace%20%24User.OldUPN%2C%24User.NewUPN)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EWhen%20I%20run%20the%20script%20I%20get%20the%20following%20output.%20It%20is%20strange%20because%20it%20seems%20to%20be%20finding%20the%20account%20but%20then%20cannot%20update%20the%20UPN.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%20C%3A%5Cscripts%26gt%3B%20C%3A%5Cscripts%5CUpdateUPN.ps1%3CBR%20%2F%3Ecmdlet%20UpdateUPN.ps1%20at%20command%20pipeline%20position%201%3CBR%20%2F%3ESupply%20values%20for%20the%20following%20parameters%3A%3CBR%20%2F%3E(Type%20!%3F%20for%20Help.)%3CBR%20%2F%3ESourceFile%3A%20C%3A%5Cscripts%5CUPNChangeTest.csv%3CBR%20%2F%3EChanging%20UPN%20%3CA%20href%3D%22mailto%3Amichaels%40my%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emikes%40mydomain%3C%2FA%3E.com%20to%20msmith%40mydomain.com%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3ESet-ADUser%20%3A%20Cannot%20find%20an%20object%20with%20identity%3A%20'Mike%20Smith'%20under%3A%20'DC%3DHouston%2CDC%3DDEH'.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3EAt%20C%3A%5Cscripts%5CUpdateUPN.ps1%3A44%20char%3A2%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3E%2B%20Set-ADUser%20-Identity%20%24UserDetails.Name%20-UserPrincipalName%20(%24UserD%20...%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3E%2B%20CategoryInfo%20%3A%20ObjectNotFound%3A%20(Mike%20Smith%3AADUser)%20%5BSet-ADUser%5D%2C%20ADIdentityNotFoundException%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23993300%22%3E%2B%20FullyQualifiedErrorId%20%3A%20ActiveDirectoryCmdlet%3AMicrosoft.ActiveDirectory.Management.ADIdentityNotFoundException%2CMicrosoft.ActiveDirectory.Management.Commands.SetADUser%3C%2FFONT%3E%3C%2FP%3E%3CP%3EPS%20C%3A%5Cscripts%26gt%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1747062%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1747305%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20UPN%20with%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1747305%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F820993%22%20target%3D%22_blank%22%3E%40michaels_IT%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EReplicated%20this%20issue%20in%20my%20lab.%20Fixed%20by%20changing%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESet-ADUser%20-Identity%20%24UserDetails.Name%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESet-ADUser%20-Identity%20%24UserDetails.SamAccountName%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EKeep%20everything%20else%20the%20same.%20UPN's%20now%20changed%20correctly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps%2C%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1755576%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20UPN%20with%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1755576%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20worked%20perfectly.%20Thanks%20for%20the%20help.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F383653%22%20target%3D%22_blank%22%3E%40HidMov%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I need to change the UPN on about 200 accounts in our company. I have a CSV file with the old and new UPNs in question. The script I am trying to use is here:

 

https://gallery.technet.microsoft.com/scriptcenter/Update-UPNs-from-CSV-0acbd718 

 

<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.128
	 Created on:   	13/11/2016 15:04
	 Created by:   	Maurice Daly	
	 Filename:     	UpdateUPN.ps1
	===========================================================================
	.DESCRIPTION
		Reads the contents of a CSV specified during the runtime command,
		then updates all users contained with their new UPN.
		within.
	.EXAMPLE
		UpdateUPN.ps1 -SourceFile "C:\Users.CSV"
		
		Source CSV must contain headings OLDUPN and NEWUPN to function

	Use : This script is provided as it and I accept no responsibility for
	any issues arising from its use.

	Twitter : @modaly_it
	Blog : https://modalyitblog.com/
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
	[parameter(Mandatory = $true, HelpMessage = "Location of CSV file containing both old and new UPNs", Position = 1)]
	[ValidateNotNullOrEmpty()]
	[ValidateScript({ Test-Path $_ })]
	[string]$SourceFile
)

# Import UPN detals
$UPNDetails = Import-Csv -Path $SourceFile

# Import Active Directory commandlets
Import-Module -Name ActiveDirectory

# Loop for each user
foreach ($User in $UPNDetails)
{
	# Get AD user details using old UPN and the update
	$UserDetails = Get-ADUser -filter * | Where-Object { $_.UserPrincipalName -eq $User.OldUPN }
	Write-Output "Changing UPN $($User.OldUPN) to $($User.NewUPN)"
	Set-ADUser -Identity $UserDetails.Name -UserPrincipalName ($UserDetails.UserPrincipalName -replace $User.OldUPN,$User.NewUPN)
}

When I run the script I get the following output. It is strange because it seems to be finding the account but then cannot update the UPN.

 

PS C:\scripts> C:\scripts\UpdateUPN.ps1
cmdlet UpdateUPN.ps1 at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
SourceFile: C:\scripts\UPNChangeTest.csv
Changing UPN mikes@mydomain.com to msmith@mydomain.com
Set-ADUser : Cannot find an object with identity: 'Mike Smith' under: 'DC=Houston,DC=DEH'.
At C:\scripts\UpdateUPN.ps1:44 char:2
+ Set-ADUser -Identity $UserDetails.Name -UserPrincipalName ($UserD ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Mike Smith:ADUser) [Set-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.SetADUser

PS C:\scripts>

2 Replies
Highlighted
Best Response confirmed by michaels_IT (New Contributor)
Solution

Hi @michaels_IT 

 

Replicated this issue in my lab. Fixed by changing

 

Set-ADUser -Identity $UserDetails.Name

 

To this:

 

Set-ADUser -Identity $UserDetails.SamAccountName

 

Keep everything else the same. UPN's now changed correctly.

 

Hope this helps,

Highlighted

That worked perfectly. Thanks for the help. @HidMov