**** Please Rate the Script / Module if you download it and like it****

 

Version 1.0.1

-- Updated the inline comments.

-- Formatting Changes.

 

I have created a PS module which i am sharing with you for changing the User Principal Name (UPN) of a user in Active Directory to their Primary SMTP Address. The Module will change the UPN of a user to match with their Primary SMTP Email ID.

Pre-requisite: Primary SMTP Address should alredy be populated in Active Directory User Attributes ProxyAddresses. The Primary SMTP Address starts with SMTP: (SMTP in Capital Letters). For example: SMTP:jatin.makhija@technethub.com. 

*******

There are three functions exported from the module Set-UPNtoPSMTPv1.0.1 which are shown in below screenshot. To import the module you have to download the module and save it in a folder and use Import-Module <module path with filename> in your Powershell Session. e.g. Import-Module c:\temp\Set-UPNtoPSMTPv1.0.1.psm1

Please make sure ActiveDirectory Powershell Module is Imported on the machine before running the Module and make sure you have required edit permissions in active directory.

PS> Import-Module ActiveDirectory

PS>  Import-Module c:\temp\Set-UPNtoPSMTPv1.0.1.psm1

PS> Get-command -module Set-UPNtoPSMTPv1.0.1

Once the Module is imported. You can use below commands on the Powershell console session to execute these functions.

PS> Set-UPNtoPSMTPOneUser <userprincipalname>

PS> Set-UPNtoPSMTPListofUsers <Text File Path Containing UPNs>

PS> Get-UPNlist

OR

PS> "userprincipalname" | Set-UPNtoPSMTPOneUser

PS>"<UPN file path>" | Set-UPNtoPSMTPListofUsers

I have created Help information for each function with examples which you can access using below commands:

***HELP INFORMATION / DOCUMENTATION***

PS> get-help Set-UPNtoPSMTPOneUser

PS> get-help Set-UPNtoPSMTPOneUser -examples

PS> Get-help Set-UPNtoPSMTPListofUsers

PS> Get-help Set-UPNtoPSMTPListofUsers -examples

PS> Get-help Get-UPNlist

*********************

PowerShell
Edit|Remove
function Set-UPNtoPSMTPOneUser 
{ 
<# 
.SYNOPSIS 
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for One User. 
 
.DESCRIPTION 
This is Set-UPNtoPSMTPOneUser Function which can be used to  
Change / match UPN to Users Primary SMTP Address for single user. 
 
.NOTES     
        Name        : Set-UPNtoPSMTPOneUser  
        Author      : Jatin Makhija  
        Version     : 1.0.1 
        DateCreated : 15-Nov-2019  
    Date Updated: 12-Dec-2019 
        Blog        : https://www.technethub.com 
         
.LINK 
https://www.technethub.com 
 
.EXAMPLE 
PS> Set-UPNtoPSMTPOneUser <userprincipalname> 
 
For Example 
 
Set-UPNtoPSMTPOneUser jmak@technethub.local 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
To Find the userprincipalName of a user from AD:  
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
 
.EXAMPLE 
 
"userprincipalname" | Set-UPNtoPSMTPOneUser 
 
For Example 
 
"Jmak@technethub.local" | Set-UPNtoPSMTPOneUser 
 
EXAMPLE 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
#> [cmdletBinding()] 
   # Defining parameter name UPN  
   param( 
    [parameter( 
     mandatory=$true, 
     Position=0, 
     Valuefrompipeline = $true, 
     HelpMessage="Please Enter User Principal Name of AD User:")] 
     [string]$UPN 
     ) 
     #Check if the AD User is Enabled or not 
     If ([bool](get-aduser -filter {(userprincipalname -eq $UPN-and (enabled -eq $true)})) 
     { 
     try { 
          Write-host Processing "$UPN" -ForegroundColor Green -BackgroundColor black 
          #Checking the Primary SMTP attribute in AD 
          $address = Get-ADUser -filter {userprincipalname -like $UPN-Properties proxyAddresses | Select -Expand proxyAddresses | Where {$_ -clike "SMTP:*"} 
          $newUPN = $address.SubString(5) 
          #Checking if the Old UPN matched with Primary SMTP Address already. 
          if ($UPN -eq $newUPN) 
          { 
           Write-Host User UPN:"$UPN" already matches Primary SMTP Address: "$newUPN" -ForegroundColor Black -BackgroundColor DarkYellow 
          } 
          Else 
          { 
           Write-host Changing to PrimarySMTP: "$newUPN" from UserPrincipalName:"$UPN" -ForegroundColor Green -BackgroundColor black  
           $UPN = ($UPN -split '@')[0] 
           #Set the New UPN of the User matching with the Primary SMTP Address 
           Set-ADUser $UPN -UserPrincipalName $newUPN 
           Write-host "Done" -ForegroundColor Green -BackgroundColor black  
          } 
         } 
     Catch { 
            Write-Host $_ -ForegroundColor Red 
           } 
      } 
      Else  
        { 
         # If UPN is not Found then Displays below message 
         Write-host Processing "$UPN" -ForegroundColor RED -BackgroundColor black    
         Write-Host "$UPN" is not found in AD OR AD Account is Disabled OR its not an User Principal Name. To Get the User Principal Name "(UPN) use the command --> Get-ADUser -filter {Displayname -like `'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname" Replace Jatin Makhija with User DisplayName -ForegroundColor DarkYellow -BackgroundColor black 
        }           
 
 } 
 
 
function Set-UPNtoPSMTPListofUsers 
{ 
<# 
.SYNOPSIS 
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for list of users in a text file. 
 
.DESCRIPTION 
This is Set-UPNtoPSMTPListofUsers Function which can be used to  
Change / match UPN to Users Primary SMTP Address for a list of users 
 
.NOTES     
        Name        : Set-UPNtoPSMTPOneUser  
        Author      : Jatin Makhija  
        Version     : 1.0.1  
        DateCreated : 15-Nov-2019  
    Date Updated: 12-Dec-2019 
        Blog        : https://www.technethub.com 
 
.LINK 
https://www.technethub.com 
 
.EXAMPLE 
PS> Set-UPNtoPSMTPListofUsers <Text File Path Containing UPNs> 
 
For Example 
 
It can be any File name containing Users UPN in a text file. As an example i am using name UPNlist.txt 
 
Set-UPNtoPSMTPListofUsers c:\temp\UPNlist.txt  
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
Result is of one of the users in UPNlist.txt file: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
To Find the userprincipalName of a user from AD:  
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
 
.EXAMPLE 
 
"<UPN file path>" | Set-UPNtoPSMTPListofUsers 
 
For Example 
 
"C:\temp\UPNlist.txt" | Set-UPNtoPSMTPListofUsers 
 
EXAMPLE 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
Result is of one of the users in UPNlist.txt file: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
#> [cmdletBinding()] 
   param( 
    [parameter( 
     mandatory=$true, 
     Position=0, 
     Valuefrompipeline = $true, 
     HelpMessage="Please Enter the File Path where you have List of AD Users UPNs")]            
     [string]$UPNListFilePath 
             ) 
    # checking the Path provided is valid or not. 
    if (!(Test-Path $UPNListFilePath)) 
    { 
     Write-Host ""$UPNListFilePath" is not a Valid Path. Please provide Path to a text file containing Users UPN Names" -ForegroundColor Red -BackgroundColor White 
    } 
    Else 
    {             
     $UPNList = Get-Content $UPNListFilePath 
        foreach ($UPN in $UPNlist) 
        { 
         #check if the user is enabled in Active Directory 
         If ([bool](get-aduser -filter {(userprincipalname -eq $UPN-and (enabled -eq $true)})) 
         { 
          try { 
               Write-host Processing "$UPN" -ForegroundColor Green -BackgroundColor black 
               #Checking the Primary SMTP attribute in AD 
               $address = Get-ADUser -filter {userprincipalname -like $UPN-Properties proxyAddresses | Select -Expand proxyAddresses | Where {$_ -clike "SMTP:*"} 
               $newUPN = $address.SubString(5) 
               # Check if the UPN Matched the Primary SMTP Address 
               if ($UPN -eq $newUPN) 
               { 
                Write-Host User UPN:"$UPN" already matches Primary SMTP Address: "$newUPN" -ForegroundColor Black -BackgroundColor DarkYellow 
               } 
               Else  
               { 
                Write-host Changing to PrimarySMTP: "$newUPN" from UserPrincipalName:"$UPN" -ForegroundColor Green -BackgroundColor black  
                $UPN = ($UPN -split '@')[0] 
                Set-ADUser $UPN -UserPrincipalName $newUPN 
                Write-host "Done" -ForegroundColor Green -BackgroundColor black  
               } 
              } 
          Catch { 
                Write-Host $_ -ForegroundColor Red 
                } 
         } 
         Else  
         { 
          Write-host Processing "$UPN" -ForegroundColor RED -BackgroundColor black 
          Write-Host "$UPN" is not found in AD OR AD Account is Disabled OR its not an User Principal Name. To Get the User Principal Name "(UPN) use the command --> Get-ADUser -filter {Displayname -like `'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname" Replace Jatin Makhija with User DisplayName -ForegroundColor DarkYellow -BackgroundColor black 
         } 
        }   
    }    
} 
 
function Get-UPNlist 
{ 
<# 
.SYNOPSIS 
To list the user principal names of all AD users 
.DESCRIPTION 
This is Get-UPNlist Function which can be used to list the UPN Names of all AD users. 
.NOTES     
        Name        : Get-UPNlist  
        Author      : Jatin Makhija  
        Version     : 1.0.0  
        DateCreated : 15-Nov-2019  
    Date Updated: 12-Dec-2019 
        Blog        : https://www.technethub.com 
.EXAMPLE 
PS> Get-UPNlist 
#> get-aduser -filter * -Properties userprincipalname | Select -ExpandProperty Userprincipalname 
}
 

Set-UPNtoPSMTPOneUser DOCUMENATION

PowerShell
Edit|Remove
<# 
.SYNOPSIS 
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for One User. 
 
.DESCRIPTION 
This is Set-UPNtoPSMTPOneUser Function which can be used to  
Change / match UPN to Users Primary SMTP Address for single user. 
 
.NOTES     
        Name       : Set-UPNtoPSMTPOneUser  
        Author     : Jatin Makhija  
        Version    : 1.0.0  
        DateCreated: 15-Nov-2019  
        Blog       : https://www.technethub.com 
         
.LINK 
https://www.technethub.com 
 
.EXAMPLE 
PS> Set-UPNtoPSMTPOneUser <userprincipalname> 
 
For Example 
 
Set-UPNtoPSMTPOneUser jmak@technethub.local 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
To Find the userprincipalName of a user from AD:  
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
 
.EXAMPLE 
 
"userprincipalname" | Set-UPNtoPSMTPOneUser 
 
For Example 
 
"Jmak@technethub.local" | Set-UPNtoPSMTPOneUser 
 
EXAMPLE 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
#>
Set-UPNtoPSMTPListofUsers DOCUMENATION
PowerShell
Edit|Remove
<# 
.SYNOPSIS 
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for list of users in a text file. 
 
.DESCRIPTION 
This is Set-UPNtoPSMTPListofUsers Function which can be used to  
Change / match UPN to Users Primary SMTP Address for a list of users 
 
.NOTES     
        Name       : Set-UPNtoPSMTPOneUser  
        Author     : Jatin Makhija  
        Version    : 1.0.0  
        DateCreated: 15-Nov-2019  
        Blog       : https://www.technethub.com 
 
.LINK 
https://www.technethub.com 
 
.EXAMPLE 
PS> Set-UPNtoPSMTPListofUsers <Text File Path Containing UPNs> 
 
For Example 
 
It can be any File name containing Users UPN in a text file. As an example i am using name UPNlist.txt 
 
Set-UPNtoPSMTPListofUsers c:\temp\UPNlist.txt  
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
Result is of one of the users in UPNlist.txt file: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
To Find the userprincipalName of a user from AD:  
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
 
.EXAMPLE 
 
"<UPN file path>" | Set-UPNtoPSMTPListofUsers 
 
For Example 
 
"C:\temp\UPNlist.txt" | Set-UPNtoPSMTPListofUsers 
 
EXAMPLE 
 
userprincipalname    = jmak@technethub.local 
primary SMTP address = jatin.makhija@technethub.com 
 
After Running the Script it would be: 
 
Result is of one of the users in UPNlist.txt file: 
 
userprincipalname    = jatin.makhija@technethub.com 
primary SMTP address = jatin.makhija@technethub.com 
 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname 
Replace Jatin Makhija with your AD user Display Name (keep the quotes). 
#>
 

Get-UPNlist DOCUMENATION
PowerShell
Edit|Remove
    <# 
.SYNOPSIS 
To list the user principal names of all AD users 
 
.DESCRIPTION 
This is Get-UPNlist Function which can be used to list the UPN Names of all AD users. 
 
.NOTES     
        Name       : Get-UPNlist  
        Author     : Jatin Makhija  
        Version    : 1.0.0  
        DateCreated: 15-Nov-2019  
        Blog       : https://www.technethub.com 
 
.EXAMPLE 
PS> Get-UPNlist 
 
 
#>
 

 

 

 


 


function Set-UPNtoPSMTPOneUser
{
<#
.SYNOPSIS
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for One User.
.DESCRIPTION
This is Set-UPNtoPSMTPOneUser Function which can be used to 
Change / match UPN to Users Primary SMTP Address for single user.
.NOTES    
        Name        : Set-UPNtoPSMTPOneUser 
        Author      : Jatin Makhija 
        Version     : 1.0.1
        DateCreated : 15-Nov-2019 
 Date Updated: 12-Dec-2019
        Blog        : https://www.technethub.com
        
.LINK
https://www.technethub.com
.EXAMPLE
PS> Set-UPNtoPSMTPOneUser <userprincipalname>
For Example
Set-UPNtoPSMTPOneUser jmak@technethub.local
userprincipalname    = jmak@technethub.local
primary SMTP address = jatin.makhija@technethub.com
After Running the Script it would be:
userprincipalname    = jatin.makhija@technethub.com
primary SMTP address = jatin.makhija@technethub.com
To Find the userprincipalName of a user from AD: 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname
Replace Jatin Makhija with your AD user Display Name (keep the quotes).
.EXAMPLE
"userprincipalname" | Set-UPNtoPSMTPOneUser
For Example
"Jmak@technethub.local" | Set-UPNtoPSMTPOneUser
EXAMPLE
userprincipalname    = jmak@technethub.local
primary SMTP address = jatin.makhija@technethub.com
After Running the Script it would be:
userprincipalname    = jatin.makhija@technethub.com
primary SMTP address = jatin.makhija@technethub.com
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname
Replace Jatin Makhija with your AD user Display Name (keep the quotes).
#> [cmdletBinding()]
   # Defining parameter name UPN 
   param(
    [parameter(
     mandatory=$true,
     Position=0,
     Valuefrompipeline = $true,
     HelpMessage="Please Enter User Principal Name of AD User:")]
     [string]$UPN
     )
     #Check if the AD User is Enabled or not
     If ([bool](get-aduser -filter {(userprincipalname -eq $UPN) -and (enabled -eq $true)}))
     {
     try {
          Write-host Processing "$UPN" -ForegroundColor Green -BackgroundColor black
          #Checking the Primary SMTP attribute in AD
          $address = Get-ADUser -filter {userprincipalname -like $UPN} -Properties proxyAddresses | Select -Expand proxyAddresses | Where {$_ -clike "SMTP:*"}
          $newUPN = $address.SubString(5)
          #Checking if the Old UPN matched with Primary SMTP Address already.
          if ($UPN -eq $newUPN)
          {
           Write-Host User UPN:"$UPN" already matches Primary SMTP Address: "$newUPN" -ForegroundColor Black -BackgroundColor DarkYellow
          }
          Else
          {
           Write-host Changing to PrimarySMTP: "$newUPN" from UserPrincipalName:"$UPN" -ForegroundColor Green -BackgroundColor black 
           $UPN = ($UPN -split '@')[0]
           #Set the New UPN of the User matching with the Primary SMTP Address
           Set-ADUser $UPN -UserPrincipalName $newUPN
           Write-host "Done" -ForegroundColor Green -BackgroundColor black 
          }
         }
     Catch {
            Write-Host $_ -ForegroundColor Red
           }
      }
      Else 
        {
         # If UPN is not Found then Displays below message
         Write-host Processing "$UPN" -ForegroundColor RED -BackgroundColor black   
         Write-Host "$UPN" is not found in AD OR AD Account is Disabled OR its not an User Principal Name. To Get the User Principal Name "(UPN) use the command --> Get-ADUser -filter {Displayname -like `'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname" Replace Jatin Makhija with User DisplayName -ForegroundColor DarkYellow -BackgroundColor black
        }          
 }
function Set-UPNtoPSMTPListofUsers
{
<#
.SYNOPSIS
Change the AD User Principal Name of a user to Match it with Primary SMTP Address for list of users in a text file.
.DESCRIPTION
This is Set-UPNtoPSMTPListofUsers Function which can be used to 
Change / match UPN to Users Primary SMTP Address for a list of users
.NOTES    
        Name        : Set-UPNtoPSMTPOneUser 
        Author      : Jatin Makhija 
        Version     : 1.0.1 
        DateCreated : 15-Nov-2019 
 Date Updated: 12-Dec-2019
        Blog        : https://www.technethub.com
.LINK
https://www.technethub.com
.EXAMPLE
PS> Set-UPNtoPSMTPListofUsers <Text File Path Containing UPNs>
For Example
It can be any File name containing Users UPN in a text file. As an example i am using name UPNlist.txt
Set-UPNtoPSMTPListofUsers c:\temp\UPNlist.txt 
userprincipalname    = jmak@technethub.local
primary SMTP address = jatin.makhija@technethub.com
After Running the Script it would be:
Result is of one of the users in UPNlist.txt file:
userprincipalname    = jatin.makhija@technethub.com
primary SMTP address = jatin.makhija@technethub.com
To Find the userprincipalName of a user from AD: 
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname
Replace Jatin Makhija with your AD user Display Name (keep the quotes).
.EXAMPLE
"<UPN file path>" | Set-UPNtoPSMTPListofUsers
For Example
"C:\temp\UPNlist.txt" | Set-UPNtoPSMTPListofUsers
EXAMPLE
userprincipalname    = jmak@technethub.local
primary SMTP address = jatin.makhija@technethub.com
After Running the Script it would be:
Result is of one of the users in UPNlist.txt file:
userprincipalname    = jatin.makhija@technethub.com
primary SMTP address = jatin.makhija@technethub.com
Get-ADUser -filter {Displayname -like 'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname
Replace Jatin Makhija with your AD user Display Name (keep the quotes).
#> [cmdletBinding()]
   param(
    [parameter(
     mandatory=$true,
     Position=0,
     Valuefrompipeline = $true,
     HelpMessage="Please Enter the File Path where you have List of AD Users UPNs")]           
     [string]$UPNListFilePath
             )
    # checking the Path provided is valid or not.
    if (!(Test-Path $UPNListFilePath))
    {
     Write-Host ""$UPNListFilePath" is not a Valid Path. Please provide Path to a text file containing Users UPN Names" -ForegroundColor Red -BackgroundColor White
    }
    Else
    {            
     $UPNList = Get-Content $UPNListFilePath
        foreach ($UPN in $UPNlist)
        {
         #check if the user is enabled in Active Directory
         If ([bool](get-aduser -filter {(userprincipalname -eq $UPN) -and (enabled -eq $true)}))
         {
          try {
               Write-host Processing "$UPN" -ForegroundColor Green -BackgroundColor black
               #Checking the Primary SMTP attribute in AD
               $address = Get-ADUser -filter {userprincipalname -like $UPN} -Properties proxyAddresses | Select -Expand proxyAddresses | Where {$_ -clike "SMTP:*"}
               $newUPN = $address.SubString(5)
               # Check if the UPN Matched the Primary SMTP Address
               if ($UPN -eq $newUPN)
               {
                Write-Host User UPN:"$UPN" already matches Primary SMTP Address: "$newUPN" -ForegroundColor Black -BackgroundColor DarkYellow
               }
               Else 
               {
                Write-host Changing to PrimarySMTP: "$newUPN" from UserPrincipalName:"$UPN" -ForegroundColor Green -BackgroundColor black 
                $UPN = ($UPN -split '@')[0]
                Set-ADUser $UPN -UserPrincipalName $newUPN
                Write-host "Done" -ForegroundColor Green -BackgroundColor black 
               }
              }
          Catch {
                Write-Host $_ -ForegroundColor Red
                }
         }
         Else 
         {
          Write-host Processing "$UPN" -ForegroundColor RED -BackgroundColor black
          Write-Host "$UPN" is not found in AD OR AD Account is Disabled OR its not an User Principal Name. To Get the User Principal Name "(UPN) use the command --> Get-ADUser -filter {Displayname -like `'Jatin Makhija'} -Properties Userprincipalname | select -ExpandProperty userprincipalname" Replace Jatin Makhija with User DisplayName -ForegroundColor DarkYellow -BackgroundColor black
         }
        }  
    }   
}
function Get-UPNlist
{
<#
.SYNOPSIS
To list the user principal names of all AD users
.DESCRIPTION
This is Get-UPNlist Function which can be used to list the UPN Names of all AD users.
.NOTES    
        Name        : Get-UPNlist 
        Author      : Jatin Makhija 
        Version     : 1.0.0 
        DateCreated : 15-Nov-2019 
 Date Updated: 12-Dec-2019
        Blog        : https://www.technethub.com
.EXAMPLE
PS> Get-UPNlist
#> get-aduser -filter * -Properties userprincipalname | Select -ExpandProperty Userprincipalname
}