PowerShell – Updating Users Email Addresses In Active Directory

Advertisement

KB ID 0001216 Dtd 05/07/16

Problem

Note: I'm referring to the Email address value that is listed on the user object in Active Directory, this will not effect any Exchange Settings!

A colleague asked me today if I had any PowerShell to update ALL the users in a clients AD, to match their UPN to their Email addresses. A quick internet search turned up loads of handy scripts to update the UPN to mach the email address, but not the way round he wanted.

AD User Name

 

Solution

In most (not all) cases your UPN is the same as your sAMaccountname and your domain name, so you can simply run the following;

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase 'DC=test,DC=net' | `
    ForEach-Object { Set-ADUser -EmailAddress ($_.samaccountname + '@test.net') -Identity $_ }

Note: Save the above as a file with a .ps1 extension, or execute both commands separately.

Update AD EMail Addresses

Now you may, (like on my test network above,) have your user logon name set to something other than firstname.lastname if so and you would prefer to set the Email value to firstname.lastname@domain.com then use the following instead.

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase 'DC=test,DC=net' | `
    ForEach-Object { Set-ADUser -EmailAddress ($_.givenName + '.' + $_.surname + '@test.net') -Identity $_ }

Note: Save the above as a file with a ps1 extension, or execute both commands separately.

Update AD EMail firstname lastname

 

Related Articles, References, Credits, or External Links

PowerShell - Update All Domain Users With Email Address From UPN

Author: PeteLong

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *