I recently had to perform an Office 365 migration for a client with hundreds of users / mailboxes with a terrible assortment of mismatched on-premises Active Directory attributes.

The script below is a script I spent quite a while writing that will fully clean up AD attributes and prepare your users for migration to Office 365 from on-prem Exchange.

The script can be downloaded below, and I’ve posted the raw code as well for review:

<# 
.SYNOPSIS 
 CleanUpADAttributesforO365.ps1 - Cleanly formats AD attributes for a migration to Office 365
 
.DESCRIPTION 
 This powershell script fully cleans up the following attributes and prepares users to be synced
 to Office 365 using Microsoft AD Connect:
 
 ♦Attribute: SamAccountName
		○ Converts the current user SamAccountName attribute to lower case. No other modifications
		  are made to this attribute.
 ♦Attribute: mail
		○ Converts the current user mail attribute to lower case. No other modifications
		  are made to this attribute.
 ♦Attribute: mailNickname
		○ Converts the current user mailNickname attribute to lower case. No other modifications
		  are made to this attribute.
 ♦Attribute: UserPrincipalName
		○ Converts the current user UserPrincipalName attribute to lower case and changes the UPN
 ♦Attribute: proxyAddresses
		○ Converts all of the current user proxyAddresses to lower case and adds an additional
		  proxyAddress for the new Office 365 .onmicrosoft.com URL, using their primary email alias
		  as the alias for the .onmicrosoft.com new proxyAddress
 
.NOTES 
┌─────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ ORIGIN STORY                                                                                │ 
├─────────────────────────────────────────────────────────────────────────────────────────────┤ 
│   DATE CREATED  : 2020.02.07 																  │
│   DATE MODIFIED : 2020.02.07 																  │
│   AUTHOR        : Vince Cantrell															  │
│   DESCRIPTION   : Initial Draft 															  │
│   VERSION	      : 1.3															              │
└─────────────────────────────────────────────────────────────────────────────────────────────┘ 
 
Please see the SWITCHES and VARIABLES sections below.
 
#>
#################################################################################################
<#
┌─────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ SWITCHES                                                                                    │   
├─────────────────────────────────────────────────────────────────────────────────────────────┤ 
│   Review the 12 switches below and set them to true or false based on your needs.			  │
│   Note: The bottom two switches cannot both be set to True (for the mail attribute), 		  │
│         pick only one.															          │
└─────────────────────────────────────────────────────────────────────────────────────────────┘ 
#>
#Switches for adding new domains / proxyAddresses. These can all be set to true:

#Add your O365 tenant domains to your on-premis AD domain as additional UPN suffixes - recommended:
$addO365DomainstoUPNSuffixesforAD = $true

#Set the switch below to true if you want to add a new proxy address for the .onmicrosoft.com domain of your O365 tenant:
$addNewProxyAddressesForO365Tenant = $true



###Switches for converting variables to lower-case. These can all be set to true:###

#Set the switch below to true if you want to convert all proxyAddresses to lower-case:
$convertProxyAddressestoLowerCase = $true

#Set the switch below to true if you want to convert their mailNickname attribute to lower case:
$convertMailNicknametoLowerCase = $true

#Set the switch below to true if you want to convert their SamAccountName attribute to lower case:
$convertSamAccountNametoLowerCase = $true

#Set the switch below to true if you want to convert their UserPrincipalName attribute to lower case:
$convertUserPrincipalNametoLowerCase = $true

#Set the switch below to true if you want to convert their mail attribute to lower case:
$convertMailAttrtoLowerCase = $true



#Switches for changing existing paramaters to new values using the variables you specify in the Variables section:

#Set the switch below to true if you want to replace the user UPN with a new domain specified below:
$changeUserUPNtoNewDomain = $true

#Set the switch below if you want to change their primary SMTP address to their UserPrincipalName @ the new UPN suffix you specify below:
$changePrimaryProxyAddrToUPNatNewDomain = $true

#Set the switch below if you want to set the mail attribute for each user to match their UserPrincipalName @ The new UPN suffix you specify below:
$changeMailAttributetoUPNwithNewDomain = $true

#Set the switch below to true if you want to set their mail attribute to match their primary SMTP proxyAddress (in case it is different):
$changeMailAttributeToPrimaryProxyAddr = $false

#################################################################################################

<#
┌─────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ VARIABLES                                                                                   │   
├─────────────────────────────────────────────────────────────────────────────────────────────┤ 
│   Review the variables below and set them based on your needs.			                  │
│   Note: If you change the log file paths to have spaces, you need to enclose them in quotes.│
│               															                  │
└─────────────────────────────────────────────────────────────────────────────────────────────┘ 
#>

#Set the variable below to the OU that you want to search within for selecting the users to modify:
$userOU = "OU=SCA Users,DC=sca,DC=titan,DC=1sourcing,DC=net"

#Set the variable below to the DN of the security group that you want to search within for selecting the users to modify:
$groupDN = "CN=TestGroup,CN=Users,DC=siskinspine,DC=com"

#Fill out the variable below if you want to filter a user search by users that already have a valid proxyAddress matching the domain suffix below.
$searchDomain = "siskinspine.com"

#Fill out the variable below if you would like to add a new UPN suffix to the domain / forest for changing the user UPN.
$newUPNsuffix = "siskinspine.com"

#Fill out the two variables below with your O365 tenant domains name without the @ if you chose to add a new SMTP Proxy Address in the above switches:
$newO365TenantDomain = "siskinspine.onmicrosoft.com"
$secondNewO365TenantDomain = "mail.siskinspine.onmicrosoft.com"



#Set the variable below to the location for the master log file:
$masterLogFile = "C:\temp\ADCleanupMasterLog.log"

#Set the variable below to the location for the log file containing the unmodified settings for each user before we change them:
$usersBeforeChangeLogFile = "C:\temp\UsersBeforeChange.log"

#Set the variable below to the location for the log file containing the "after" version of the settings for each user to compare with the before file:
$usersAfterChangeLogFile = "C:\temp\UsersAfterChange.log"




#################################################################################################

<#
┌─────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ Search OU                                                                                   │   
├─────────────────────────────────────────────────────────────────────────────────────────────┤ 
│   Use the uncommented line below if you want to search by group.			                  │
│   If you want to search by user OU, comment the first line and uncomment the second line    │
│   Alternatively, if you want to filter a users search by users that already have an         │
│   existingproxyAddress that matches a given domain, comment the first two lines and         │
│   uncomment the third line, making sure you filled out the $searchDomain variable.          │
│   You do not need to fill out the $userOU or $groupDN variables when using the third line.  │
└─────────────────────────────────────────────────────────────────────────────────────────────┘ 
#>

$usersInitial = Get-ADUser -filter {Memberof -eq $groupDN} -Properties * | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname
#$usersInitial = Get-ADUser -Filter * -SearchBase $userOU -Properties * | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname
#$usersInitial = Get-ADUser -Filter * -Properties * | Where-object {$_.ProxyAddresses -clike "SMTP:*@$searchDomain"} | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname


<#
┌─────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ END OF CUSTOMIZATION!                                                                       │   
├─────────────────────────────────────────────────────────────────────────────────────────────┤ 
│       			                                                                          │
│           !!!!!!!!!!!! DO NOT MODIFY ANYTHING BELOW THIS LINE !!!!!!!!!!!!                  │
│               															                  │
└─────────────────────────────────────────────────────────────────────────────────────────────┘ 
#>


#################################################################################################
#################################################################################################

#PRESECTION: Start of script. Get domain and UPN Suffixes and set the timestamp for all log files.

#################################################################################################
#################################################################################################

#First things first, check our dependencies:
if ($changeMailAttributeToPrimaryProxyAddr -and $changeMailAttributetoUPNwithNewDomain)
{
    Write-Warning "You have set both of the 'change' switches for the mail attribute to 'True' - Only one can be set to true. Please fix or the script will not run."
    Write-Warning "Exiting"
    EXIT
}
#Get the current time and format it for the log file names:
$timestamp = Get-Date -UFormat "%Y-%m-%d--%T" | ForEach-Object { $_ -replace ":", "-" }
#Append the date and time to the master log file:
$masterLogDirName  = [io.path]::GetDirectoryName($masterLogFile)
$masterLogFileName = [io.path]::GetFileNameWithoutExtension($masterLogFile)
$masterLogExt      = [io.path]::GetExtension($masterLogFile)
$masterLogFile     = "$masterLogDirName\$masterLogFileName--$timestamp$masterLogExt"
#Append the date and time to the untouched users log file:
$untouchedUsersLogDirName  = [io.path]::GetDirectoryName($usersBeforeChangeLogFile)
$untouchedUsersLogFileName = [io.path]::GetFileNameWithoutExtension($usersBeforeChangeLogFile)
$untouchedUsersLogExt      = [io.path]::GetExtension($usersBeforeChangeLogFile)
$usersBeforeChangeLogFile  = "$untouchedUsersLogDirName\$untouchedUsersLogFileName--$timestamp$untouchedUsersLogExt"
#Append the date and time to the modified users log file:
$modifiedUsersLogDirName  = [io.path]::GetDirectoryName($usersAfterChangeLogFile)
$modifiedUsersLogFileName = [io.path]::GetFileNameWithoutExtension($usersAfterChangeLogFile)
$modifiedUsersLogExt      = [io.path]::GetExtension($usersAfterChangeLogFile)
$usersAfterChangeLogFile  = "$modifiedUsersLogDirName\$modifiedUsersLogFileName--$timestamp$modifiedUsersLogExt"

#################################################################################################
#Read off the selected options and prompt for confirmation to proceed:
#################################################################################################
"You chose the following options for this script:" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
if ($addNewProxyAddressesForO365Tenant)
	{
		"addNewProxyAddressesForO365Tenant: You chose to add a new proxyAddresses for each user for your O365 domains: $newO365TenantDomain and $secondNewO365TenantDomain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}

if ($addO365DomainstoUPNSuffixesforAD)
	{
		"addO365DomainstoUPNSuffixesforAD: You chose to add a your O365 tenant domains as domain suffixes in your AD: $newO365TenantDomain and $secondNewO365TenantDomain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($convertProxyAddressestoLowerCase)
	{
		"convertProxyAddressestoLowerCase: You chose to convert the proxyAddress for each user " | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($convertMailNicknametoLowerCase)
	{
		"convertMailNicknametoLowerCase: You chose to convert the mailNickname attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($convertSamAccountNametoLowerCase)
	{
		"convertSamAccountNametoLowerCase: You chose to convert the SamAccountName attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($convertUserPrincipalNametoLowerCase)
	{
		"convertUserPrincipalNametoLowerCase: You chose to convert the UserPrincipalName attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($convertMailAttrtoLowerCase)
	{
		"convertMailAttrtoLowerCase:  You chose to convert the mail attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($changeUserUPNtoNewDomain)
	{
		"changeUserUPNtoNewDomain: You chose to set the UPN suffix for each user to match the new domain you specified: $newUPNsuffix - This also adds the UPN to your AD suffix list." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($changeMailAttributetoUPNwithNewDomain)
	{
		"changeMailAttributetoUPNwithNewDomain:  You chose to change the mail attribute for each user to UserUPN@$newUPNsuffix" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($changeMailAttributeToPrimaryProxyAddr)
	{
		"changeMailAttributeToPrimaryProxyAddr:  You chose to change the mail attribute for each user to match their primary proxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
if ($changePrimaryProxyAddrToUPNatNewDomain)
	{
		"changePrimaryProxyAddrToUPNatNewDomain:  You chose to change the primary proxyAddress attribute for each user to SMTP:UserUPN@$newUPNsuffix - the old primary proxyAddress will be set as an alias." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}

$confirmation = Read-Host "Are you sure you want to proceed with the changes above?: `n[Y]es or [N]o"
if ($confirmation -eq 'y') {
     "You pressed 'y' for Yes - Proceeding with script." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
      



#################################################################################################
#################################################################################################

#Section 1: Check if new UPN already exitst in domain, and add it to domain if not.

#################################################################################################
#################################################################################################

if ($changeUserUPNtoNewDomain)
	{
		$domain = Get-ADDomain  
		$UPNsuffix = Get-ADforest $domain.dnsroot | select UPNSuffixes  
		if ($UPNsuffix.UPNSuffixes -eq $newUPNsuffix)
			{  
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"Result: The new UPN you entered: | $newUPNsuffix | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				Start-Sleep -Seconds 2  
			}  
		else
			{  
				Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$newUPNsuffix}
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"Result: The new UPN suffix you entered: | $newUPNsuffix | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
				Start-Sleep -Seconds 2  
				"Result: The new UPN suffix | $newUPNsuffix | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				Start-Sleep -Seconds 2 
			}   
	}
else
	{
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"Result: You opted not to change the UPN for your users, so this section was skipped." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
#################################################################################################



#################################################################################################
#################################################################################################

#Section 2: Check if O365 domains already exitst in domain, and add them to domain if not.

#################################################################################################
#################################################################################################

if ($addO365DomainstoUPNSuffixesforAD)
	{
		$domain = Get-ADDomain  
		$UPNsuffix = Get-ADforest $domain.dnsroot | select UPNSuffixes  
		if ($UPNsuffix.UPNSuffixes -eq $newO365TenantDomain)
			{  
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`---Section 2: Add Primary O365 tenant domain suffix to domain---------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				  
			}
		else
			{  
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`---Section 2: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
				Start-Sleep -Seconds 2 
				Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$newO365TenantDomain}
				"Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				Start-Sleep -Seconds 2  
			}
		if ($UPNsuffix.UPNSuffixes -eq $secondNewO365TenantDomain)
			{
				"Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				Start-Sleep -Seconds 2 
			}
		else
			{  
				"Result: The O365 secondary tenant domain you entered: | $secondNewO365TenantDomain | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
				Start-Sleep -Seconds 2 
				Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$secondNewO365TenantDomain}
				"Result: The O365 secondary tenant domain you entered: | $secondNewO365TenantDomain | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				Start-Sleep -Seconds 2  
			}
	}
else
	{
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`---Section 2: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"Result: You opted not to add the O365 tenant domains as UPN suffix options for your AD, so this section was skipped." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
	}
#################################################################################################

#Let's save a header to the "Before Change" list of user properties
"`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile
"`---List of users and settings prior to modification on $timestamp-------------------------" >> $usersBeforeChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile
$initialUser >> $usersBeforeChangeLogFile

#################################################################################################
#################################################################################################

#Section 3: Modify the users. 

#################################################################################################
#################################################################################################

"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
"`---Section 3: Modify the users----------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
"`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow


Foreach ($initialUser in $usersInitial) 
    {
		"Name: $($initialUser.Name)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
		#################################################################################################
		#SECTION - USER PRINCIPAL NAME
		#################################################################################################
		if ($changeUserUPNtoNewDomain)
			{
				if ($convertUserPrincipalNametoLowerCase)
					{
						#Set a new variable that has their UPN with the new UPN suffix converted to lower case:
						$newUpn = $initialUser.UserPrincipalName.Replace($initialUser.UserPrincipalName.Split("@")[1],$newUPNsuffix).ToLower()
						"Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
                        Start-Sleep -Seconds 1   
						"New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
                        Start-Sleep -Seconds 1  
						Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn
					}
				else
					{
						#Set a new variable that has their UPN with the new UPN suffix:
						$newUpn = $initialUser.UserPrincipalName.Replace($initialUser.UserPrincipalName.Split("@")[1],$newUPNsuffix)
						"Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
                        Start-Sleep -Seconds 1  
						"New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
                        Start-Sleep -Seconds 1  
						Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn
					}
			}
		else
			{
				if ($convertUserPrincipalNametoLowerCase)
					{
						#Set a new variable that has their UPN with the new UPN suffix converted to lower case:
						$newUpn = $initialUser.UserPrincipalName.ToLower()
						"Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
                        Start-Sleep -Seconds 1   
						"New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
                        Start-Sleep -Seconds 1  
						Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn
					}
				else
					{
						#Set a new variable that has their UPN with the new UPN suffix:
						$newUpn = $initialUser.UserPrincipalName
						"Unmodified UserPrincipalName for user: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
                         Start-Sleep -Seconds 1 
					}
			}
		#################################################################################################
		#SECTION - SAM ACCOUNT NAME
		#################################################################################################
		if ($convertSamAccountNametoLowerCase)
			{
				#Set a new variable that has their existing SAN converted to lower case:
				$newSAN = $initialUser.SamAccountName.ToLower()
				"Current SamAccountName: $($initialUser.SamAccountName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
                Start-Sleep -Seconds 1 
				"New SamAccountName: $($newSAN)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
				Start-Sleep -Seconds 1 				
				Set-ADUser $initialUser.SamAccountName -Replace @{SamAccountName = $newSAN}
			}
		#################################################################################################
		#SECTION - PROXY ADDRESSES
		#################################################################################################
		#Get the user proxy addresses into a variable:
		$oldProxyAddresses = (Get-ADUser $initialUser.SamAccountName -Properties proxyaddresses).proxyaddresses

		#Check if we're changing the primary proxyAddress or not:
		if ($changePrimaryProxyAddrToUPNatNewDomain)
			{
				#If we are, set the new primary proxyAddress equal to the newUPN with SMTP in front:
				$newPrimaryProxyAddress = "SMTP:"+$newUpn
				#Set a new variable with lower-case SMTP that we can use to check if the new desired primary email exists already as an alias:
				$newSecondaryProxyAddress = "smtp:"+$newUpn
				$existingPrimaryProxyAddress = $newUpn
				#Check if the desired new primary proxyAddress is already set as the primary proxyAddress:
				if ($oldProxyAddresses -ccontains $newPrimaryProxyAddress)
                    {
						#If so, let's log that:
                        "User primary proxyAddress is already set to $($newPrimaryProxyAddress) - no change necessary." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
						Start-Sleep -Seconds 1 
						#Next let's see if we're supposed to convert it to lower case:
						if ($convertProxyAddressestoLowerCase)
							{
								#If we are, let's parse each proxyAddress, splitting our existing proxyAddress by colon:
								$newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newPrimaryProxyAddress })
								$newProxyAddresses = 
								foreach($newPaddress in $newProxyAddresses)
									{
										$newPaddress.ToLower() 
									}
								$newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower())
                                #$newPrimaryProxyAddress
								#Let's read out the old and new proxyAddresses to the console and log:
								"Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
								Start-Sleep -Seconds 1 
								"New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 	
								Start-Sleep -Seconds 1 								
								Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
								Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress}
							}
						#Since the desired new primary proxyAddress is already primary and we aren't supposed to convert them to lower-case, nothing needs to be done:
						else
							{
								"Unmodified proxyAddresses for user: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
							}
                    }
				#We know the new desired UPN isn't the primary proxyAddress, let's check if it's set as an alias already:
			
				elseif ($oldProxyAddresses -ccontains $newSecondaryProxyAddress)
					{
						#If so, let's log that:
						"The desired new Primary proxyAddress for the user already exists as a secondary alias. We'll set it as primary." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
						Start-Sleep -Seconds 1 
						#Next let's see if we're supposed to convert the proxyAddresses to lower-case:
						if ($convertProxyAddressestoLowerCase)
							{
								#If we are, let's parse each proxyAddress, splitting our existing proxyAddress by colon:
								$newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newSecondaryProxyAddress })
								$newProxyAddresses = 
								foreach($newPaddress in $newProxyAddresses)
									{
										$newPaddress.ToLower() 
									}
								$newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower())
								#Let's read out the old and new proxyAddresses to the console and log:
								"Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
								Start-Sleep -Seconds 1 
								"New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								"With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
								Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress}
							}
						#If we aren't supposed to convert the proxyAddresses to lower-case, we still need to split our desired new primary proxyAddress out from the existing aliases:
						else
							{
								$newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newSecondaryProxyAddress })
								#Let's read out the old and new proxyAddresses to the console and log:
								"Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								"New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
								Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress}
							}
					}
				#Lastly, if the new desired UPN does not exist as either the primary nor any of the secondary proxyAddresses, let's proceed without it and add it in:
				else
					{
						#Let's see if we're supposed to make the proxyAddresses lower-case or not:
						if ($convertProxyAddressestoLowerCase)
							{
								#If so, we can just convert all proxyAddresses to lower-case since we'll be adding a new primary: 
								$newProxyAddresses =
								foreach($oldAddress in $oldProxyAddresses)
									{
										$oldAddress.tolower()
									}
								$newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower())
								#Let's read out the old and new proxyAddresses to the console and log:
								"Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								"New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 	
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
								Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress}
							}
						#Since we aren't supposed to convert the proxyAddresses to lower-case, we still need to parse them so we can convert the SMTP: for the existing primary proxyAddress to lower-case to make it an alias:
						else
							{
								Write-Host "Proxy Road 13"
								$newProxyAddresses = $oldProxyAddresses -Replace 'SMTP:','smtp:'
								#Let's read out the old and new proxyAddresses to the console and log:
								"Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
								Start-Sleep -Seconds 1 
								"New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 	
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
								Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress}
							}
					}

			}
		else
			{
				if ($convertProxyAddressestoLowerCase)
			        {
						#For each proxy address, make it lower case:
						$newProxyAddresses = 
						foreach($oldAddress in $oldProxyAddresses)
							{
								#Let's make an exemption for the primary SMTP: address (the one that has SMTP: in caps) so we don't accidently remove their primary by converting it to lower case:
								if ($oldAddress.StartsWith("SMTP:"))
									{
										#Convert the second part of their primary address to lower case:
										$newPrimaryProxyAddress = $oldAddress.Replace($oldAddress.Split(":")[1],$oldAddress.Split(":")[1].ToLower())
										#Convert the second part of their primary address to lower case:
										$existingPrimaryProxyAddress = $oldAddress.Split(":")[1]
										#Set the new lower-case second part of their primary SMTP address to a variable:
										$primaryAlias = $existingPrimaryProxyAddress.Split("@")[0]
									}
								else
									{
										#Convert the non-primary SMTP addresses to lower case:
										$oldAddress.tolower()
									}
							}
				        "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
						Start-Sleep -Seconds 1 
				        "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
						Start-Sleep -Seconds 1 
				        "With a new primary proxy address of: SMTP:$($existingPrimaryProxyAddress)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
						Start-Sleep -Seconds 1 
				        Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses}
				        Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="SMTP:"+$existingPrimaryProxyAddress}
			        }
				else
					{
						$temp1 = 
						foreach($oldAddress in $oldProxyAddresses)
							{
								if ($oldAddress.StartsWith("SMTP:"))
									{
										$existingPrimaryProxyAddress = $oldAddress.Split(":")[1]
										$primaryAlias = $existingPrimaryProxyAddress.Split("@")[0]
									}
							}
						"Unmodified proxyAddresses for user: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
						Start-Sleep -Seconds 1 
					}
			}
		#################################################################################################
		#SECTION - MAIL ATTRIBUTE
		#################################################################################################
		#Check to see if the mail attribute is set for this user and if not, set it to primary SMTP address:
		if (!$initialUser.mail)
			{
				#First check if we're supposed to convert the mail attribute to lower-case:
				if ($convertMailAttrtoLowerCase)
					{
						#If so, check if we are supposed to set the mail attribute to their primary proxyAddress:
						if ($changeMailAttributeToPrimaryProxyAddr)
							{
								$newmailAttr = $existingPrimaryProxyAddress.ToLower()
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If not, check if we are supposed to set the mail attribute to their their new UPN:
						elseif ($changeMailAttributetoUPNwithNewDomain)
							{
								$newmailAttr = $newUpn.ToLower()
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If neither of those are true, set their missing mail attribute to their existing UPN:
						else
							{
								$newmailAttr = $initialUser.UserPrincipalName.ToLower()
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
					}
				else
					{
						#If we aren't supposed to convert the case, check if we are supposed to set the mail attribute to their primary proxyAddress:
						if ($changeMailAttributeToPrimaryProxyAddr)
							{
								$newmailAttr = $existingPrimaryProxyAddress
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If not, check if we are supposed to set the mail attribute to their their new UPN:
						elseif ($changeMailAttributetoUPNwithNewDomain)
							{
								$newmailAttr = $newUpn
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If neither of those are true, set their missing mail attribute to their existing UPN:
						else
							{
								$newmailAttr = $initialUser.UserPrincipalName
								"User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
					}
			}
		#Assuming the mail attribute isn't missing:
		else
			{
				#First check if we're supposed to convert the mail attribute to lower-case:
				if ($convertMailAttrtoLowerCase)
					{
						#If so, check if we are supposed to set the mail attribute to their primary proxyAddress:
						if ($changeMailAttributeToPrimaryProxyAddr)
							{
								$newmailAttr = $existingPrimaryProxyAddress.ToLower()
								"Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
								Start-Sleep -Seconds 1 
								"New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If not, check if we are supposed to set the mail attribute to their their new UPN:
						elseif ($changeMailAttributetoUPNwithNewDomain)
							{
								$newmailAttr = $newUpn.ToLower()
								"Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If neither of those are true we'll leave the mail attribute alone:
						else
							{
								"Unmodified mail attribute for user: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
							}
					}
				else
					{
						#If we aren't supposed to convert the case, check if we are supposed to set the mail attribute to their primary proxyAddress:
						if ($changeMailAttributeToPrimaryProxyAddr)
							{
								$newmailAttr = $existingPrimaryProxyAddress
								"Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								"New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If not, check if we are supposed to set the mail attribute to their their new UPN:
						elseif ($changeMailAttributetoUPNwithNewDomain)
							{
								$newmailAttr = $newUpn
								"Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
								Start-Sleep -Seconds 1 
								"New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
								Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr}
							}
						#If neither of those are true we'll leave the mail attribute alone:
						else
							{
								"Unmodified mail attribute for user: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
								Start-Sleep -Seconds 1 
							}
					}
			}
		#################################################################################################
		#SECTION - ADD NEW PRIMARY PROXY ADDRESS IF SWITCH IS SET
		#################################################################################################
		if ($addNewProxyAddressesForO365Tenant)
			{
				"Adding a new proxyaddress of: smtp:$($initialUser.SamAccountName)@$($newO365TenantDomain)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
				Start-Sleep -Seconds 1 
				Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="smtp:"+$initialUser.SamAccountName+"@"+$newO365TenantDomain}	
                "Adding a second new proxyaddress of: smtp:$($initialUser.SamAccountName)@$($secondNewO365TenantDomain)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
				Start-Sleep -Seconds 1 
				Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="smtp:"+$initialUser.SamAccountName+"@"+$secondNewO365TenantDomain}
			}
		#################################################################################################
		#SECTION - MAIL NICKNAME
		#################################################################################################
		#Check if they have an existing mailNickname, if not move on:
		if ($initialUser.mailNickname)
			{
				#Check switches to see if we are supposed to convert mailNickname to lower case:
				if ($convertMailNicknametoLowerCase)
					{
						#Convert the mailNickname to lower case.
						$newmailNick = $initialUser.mailNickname.ToLower()
						"Current mailNickname: $($initialUser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow  
						Start-Sleep -Seconds 1 
						"New mailNickname: $($newmailNick)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow 
						Start-Sleep -Seconds 1 
						Set-ADUser $initialUser.SamAccountName -Replace @{mailNickname = $newmailNick}
					}
				else
					{
						"Unmodified mailNickname attribute for user: $($initialUser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
						Start-Sleep -Seconds 1 
					}
			}
		else
			{
				"The mailNickname attribute was not set for this user." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
				Start-Sleep -Seconds 1 
			}

        "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
		Start-Sleep -Seconds 1 
        "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
    }

#################################################################################################
#################################################################################################

#END OF USER MODIFICATIONS

#################################################################################################
#################################################################################################

#Let's save a header to the "After Changes" list of user properties
"`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile
"`---List of users and settings after modification on $timestamp----------------------------" >> $usersAfterChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile
"`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile


$usersChanged = $usersInitial
$usersChanged >> $usersAfterChangeLogFile
Foreach ($newuser in $usersChanged) 
    {
        "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "Final Output:" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "`nName: $($newuser.Name)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "SamAccountName: $($newuser.SamAccountName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "EmailAddress: $($newuser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "mailNickname: $($newuser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "UserPrincipalName: $($newuser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "ProxyAddresses: $((get-aduser $newuser.SamAccountName -Properties proxyaddresses).proxyaddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
        "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
    }

"#################################################################################################" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
"`n"  | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
"Execution of this script is now complete. Thank you for using this powerscript!" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
"`n"  | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
"#################################################################################################" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow   
Start-Sleep -Seconds 3 

}
else
    {
         "You pressed a key other than Y - Exiting" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow
    }