Recursive / Mass AD Primary Email change / update
for this to work you must have update the UPN in active directory for the target
users to the proper primary UPN.
Note: Regardless of the account EVERY account should have a valid defined UPN
or this will cause problems with many different products.
Source
clear
import-module activedirectory
$Tstamp = Get-Date -uFormat "%Y_%m_%d %H:%M:%S"
$SearchOUs = @("ou=faculty-staff,dc=school,dc=edu","ou=students,dc=school,dc=edu")
$NewSuffix = "bigschool.edu"
Foreach ($OU in $SearchOUs) {
$allusers = Get-ADuser -Filter * -SearchBase $OU
ForEach ($myuser in ($allusers | where { $_.userprincipalname -notlike ("*@" + $NewSuffix) } )) {
if (($myuser.UserPrincipalName -ne $null) -and ($myuser.UserPrincipalName.length -gt 0)){
$myuser | Set-ADUser -UserPrincipalName ( $myuser.UserPrincipalName.split("@")[0] + "@" + $NewSuffix ) -EmailAddress ( $myuser.UserPrincipalName.split("@")[0] + "@" + $NewSuffix )
($tstamp + " " + $Myuser.SamAccountName + " Assigned UPN (1) : " + ( $myuser.UserPrincipalName.split("@")[0] + "@" + $NewSuffix )) | Add-Content -path ($logpath + "UserMaintenance.log")
} else { #null upn, recreate and set
$myuser | Set-ADUser -UserPrincipalName ( $myuser.SamAccountName + "@" + $NewSuffix ) -emailaddress ( $myuser.SamAccountName + "@" + $NewSuffix )
($tstamp + " " + $Myuser.SamAccountName + " Assigned UPN (2) : " + ( $myuser.SamAccountName + "@" + $NewSuffix )) | Add-Content -path ($logpath + "UserMaintenance.log")
}
}
}