Powershell script to set default email for all AD users

Depending on the information stored for the users in the AD, there is two ways this can be accomplished:

1.Set email field and default email in ProxyAddresses according to UserPrincipalName:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM"
foreach ($user in $users) {
$useremail = $user.UserPrincipalName
Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )} -EmailAddress $useremail
}
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM" foreach ($user in $users) { $useremail = $user.UserPrincipalName Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )} -EmailAddress $useremail }
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM"
foreach ($user in $users) {
    $useremail = $user.UserPrincipalName
    Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )} -EmailAddress $useremail
}

2. Set default email in ProxyAddresses according to email field:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM"
foreach ($user in $users) {
$useremail = $user.EmailAddress
Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )}
}
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM" foreach ($user in $users) { $useremail = $user.EmailAddress Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )} }
$users = Get-ADUser -Filter * -properties EmailAddress -SearchBase "ou=Test,ou=Departments,dc=DOMAIN,dc=COM"
foreach ($user in $users) {
    $useremail = $user.EmailAddress
    Set-ADUser $user -Add @{'ProxyAddresses' = ("SMTP:"+$useremail )}
}