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!

Advertisements

4 Responses to Bulk copy SAMAccountName to Alias using PowerShell

  1. Jonas says:

    I’ve seen many customers running Exchange where they tend to change sAMAccountName/UPN username but forget to change Exchange Alias. Sure, it works but just cause confusion for us admins. Would you say it’s a smart solution to schedule this script to run every hour/day to make sure sAMAccountName is always the same as Exchange Alias?

    • Martin Sundström says:

      Why not, it would definitely solve the issue. But if the customer really want to solve it the right way I would suggest that they change their process for creating accounts. If they do it manually they should update their checklist for mailbox creation. If they use a portal or tool that should be updated to match the new requirements.
      But you can of course use a scheduled script as a compliment to any of the other solutions above, just to be sure :)

      • Jonas says:

        Yes, I always ask them to change that too but with 20.000+ mailboxes and ~100 admins, they tend to forget it :). It would be interesting to see what happens with sAMAccountName and Exchange Alias attributes in the future, maybe one of them will be deprecated. sAMAccountName is old and Exchange Alias feels like it was more necessary in E2003 and older. What do you think? I think UPN would be enough as long as you don’t have applications using the other two.

      • Martin Sundström says:

        Yes many of them tend to forget that :)

        It is probably not that easy but at least Alias is kind of unnecessary now days. As you mention, it was used more in earlier versions. And as we move more towards using id to identify mailboxes in Exchange 2013, Alias is even more unnecessary… My guess is that if Microsoft can spare the resources to clean up some of the attributes we will see even more of them removed in the future…

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

%d bloggers like this: