Forum Discussion

michaels_IT's avatar
michaels_IT
Copper Contributor
Oct 05, 2020

Changing UPN with Powershell

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>

  • 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,

  • HidMov's avatar
    HidMov
    Steel Contributor

    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,

Resources