Share via


PowerShell: Update mail and mailNickname for all users in OU

Below commands will come in handy if you need to update the mail and mailNickname (alias) attributes of Active Directory users in an OU.

In the below commands have copied the sAMAccountName as the value. You may modify as you need.

Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=OUName,DC=domain,DC=com" |  Foreach-Object {Set-ADUser -Identity $_ -Email "$($_.samaccountname)@domain.com" }

Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=OUName,DC=domain,DC=com" |  ForEach-Object {Set-ADUser -Identity $_ -Replace @{mailNickname=$_.samaccountname}}

Hope this comes in handy for someone out there.