Bulk copy SAMAccountName to Alias using PowerShell

Just a short one to show how to copy the SAMAccountName to Alias using PowerShell. You can easily change the initial population of $Mailboxes and test the script on a single, or smaller number of users.

You also need to remove the Write-Host commands and -ErrorAction Inquire –Whatif in the Set-Mailbox command after your testing.

During testing:

$Mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox

foreach ($Mailbox in $Mailboxes)

{
	$SAMAccountName = $Mailbox.SAMAccountName
	$OldAlias = $Mailbox.Alias

	if ($SAMAccountName -ne $OldAlias)
	{
		Write-Host $Mailbox.SAMAccountName
		Write-Host $Mailbox.Alias
		
		Set-Mailbox $Mailbox -Alias “$SAMAccountName” -Confirm:$false -ErrorAction Inquire -Whatif
		
		Write-Host
	}
}

After testing:

$Mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox

foreach ($Mailbox in $Mailboxes)

{
	$SAMAccountName = $Mailbox.SAMAccountName
	$OldAlias = $Mailbox.Alias

	if ($SAMAccountName -ne $OldAlias)
	{
		Set-Mailbox $Mailbox -Alias “$SAMAccountName” -Confirm:$false
	}
}

Verifying the changes:

Listing all mailboxes where SAMAccountName and Alias differs…

Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Where {$_.SAMAccountName -ne $_.Alias}

Let me know if you have any questions, thanks for reading!