Fix Office 365 UPN

Today I am sharing a script that We have written to fix office 365 UPN.

This can help the organizations moving to office 365 & want to update the UPN in phases rather than big bang approach.

Prerequisites: Exchange management Shell

Extract the script from below link & update the text file with samaccountnames of the users for whom you want to match the UPN with SMTP address.

https://gallery.technet.microsoft.com/scriptcenter/Fix-Office-365-UPN-abb1a62f

Run the batch file, script will log as well as report in respective folders.

Report will be as below:

######################################################################### 
#            Author: Vikas Sukhija 
#            Reviewer: 
#            date: 7/6/2016 
#            Modified:7/11/2016                         
#            Description: Fix o365 UPN 
#            Update: Added reporting 
######################################################################### 
 
$date1 = get-date -format d 
$date1 = $date1.ToString().Replace("/","-"$time = get-date -format t 
 
$time = $time.ToString().Replace(":""-"$time = $time.ToString().Replace(" """) 
 
$logs = ".\Logs" + "\" + "Processed_" + $date1 + "_" + $time + "_.log" 
$report = ".\report" + "\" + "Report_" + $date1 + "_" + $time + "_.csv" 
 
$collection = @() 
 
Start-Transcript -Path $logs 
 
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $null) 
{ 
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
} 
 
 
$users = get-content .\users.txt 
 
$users | foreach-object{ 
 
$user = $_ 
 
$mbx=Get-User $user | Where { -Not [string]::IsNullOrEmpty($_.WindowsEmailAddress) }  
 
$mbcoll = "" | select Name,ExistingUPN,WindowsEmailAddress,ModifiedUPN 
 
if($mbx){ 
 
    if($mbx.UserPrincipalName -eq $mbx.WindowsEmailAddress) { 
 
    Write-host "$user is already setup as "$mbx.UserPrincipalName"" -foregroundcolor blue  
    $mbcoll.Name = $mbx.Name 
    $mbcoll.ExistingUPN = $mbx.UserPrincipalName 
    $mbcoll.WindowsEmailAddress = $mbx.WindowsEmailAddress 
    $mbcoll.ModifiedUPN = "Not Modified" } 
 
    else{ 
 
    Set-User -identity $mbx -UserPrincipalName $mbx.WindowsEmailAddress.ToString() 
    Write-host "Updating UPN for $user as "$mbx.WindowsEmailAddress"" -foregroundcolor green 
    #For($i = 1; $i -le "5"; $i++){ sleep 1;Write-Progress -Activity "Updating UPN for $user" -status "$i" -percentComplete ($i /10*100)} 
    #$mbx1 = get-user -identity $mbx 
    $mbcoll.Name = $mbx.Name 
    $mbcoll.ExistingUPN = $mbx.UserPrincipalName 
    $mbcoll.WindowsEmailAddress = $mbx.WindowsEmailAddress 
    $mbcoll.ModifiedUPN = $mbx.WindowsEmailAddress } 
        } 
 
else{ 
 
$mbx=Get-User $user 
write-host "$user ..doesn't have ..WindowsEmailAddress" -foregroundcolor magenta 
    $mbcoll.Name = $mbx.Name 
    $mbcoll.ExistingUPN = $mbx.UserPrincipalName 
    $mbcoll.WindowsEmailAddress = $mbx.WindowsEmailAddress 
    $mbcoll.ModifiedUPN = "Empty Windows Email"$collection +$mbcoll 
} 
 
$collection | export-csv $report -notypeinfo 
 
stop-transcript 
####################################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

One thought on “Fix Office 365 UPN

  1. Pingback: Fix Office 365 UPN – Run Daily | Microsoft Technologies Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s