Friday, April 18, 2014

Rename Exchange-AD User with Power Shell Script

This is probably not the most elegant PS script you'll ever see, and will most likely have experienced coders laughing and squirting milk from their noses (or whatever experienced coders drink) ...but hey..it works!

I searched quite a bit to find a script that would take care of all the attributes needed to rename a AD\Exchange user's surname, truncate it to fit our naming convention, rename their home directory, email addresses etc from the command line....all to no avail. So I pieced together what you see below. There's comments and echos for every function\ line, so it's easy to follow.

Now, I'm just waiting for somebody to get to get married so I can put this thing to work! Also, thanks to all the references (see comments in script) and their respective sites and authors that really helped me out....

Function Begin {            
Clear-Host            
write-host "***********************************************************************" -ForegroundColor Red            
Write-host "Script by created by Dennis Topo Jr 2014" -ForegroundColor Cyan            
write-host "***********************************************************************" -ForegroundColor Red            
write-host             
$firstname=Read-Host "Enter current First name of user to be renamed (ie. John)"            
write-host            
$lastname=Read-Host "Enter current Last name of user to be renamed (ie. Smith)"            
$fullname="$firstname" +" " + "$lastname"            
            
get-aduser -Filter {name -eq $fullname}            
            
$Choice=Read-Host "Do you want to rename the above user? (if you don't see any output, user is NOT in AD, or you typed it wrong!) Yes(Y) or No(N)?"            
If (($Choice -eq 'Y') -or ($Choice -eq 'y')) {Prompt1}            
if (($Choice -eq 'N') -or ($Choice -eq 'n')) {Begin}            
Else {Begin}            
            
            
}            
                        
Function Prompt1 {            
write-host            
$newsur=Read-host "Enter NEW Sur Name for User"            
write-host            
            
# set first initial of user account name            
$firstsam=$firstname.substring(0,1)            
            
# set CURRENT sur name to 7 chraraters only            
# count the characters in the last name             
$count=Measure-Object -InputObject $lastname -Character | select -expand Characters             
             
# If the count is less than 7, use that number, if not, set it to 7            
If ($count -lt 7 ){$numberx = $count}Else{$numberx = 7}            
$sn7 = $lastname.Substring(0, $numberx)            
            
            
$samaccount=$firstsam+$sn7            
            
            
# set NEW sur name to 7 chraraters only            
# count the characters in the last name             
$count=Measure-Object -InputObject $newsur -Character | select -expand Characters             
             
# If the count is less than 7, use that number, if not, set it to 7            
If ($count -lt 7 ){$numberx = $count}Else{$numberx = 7}            
$newsn7 = $newsur.Substring(0, $numberx)            
            
write-host "***********************************************************************" -ForegroundColor Green            
Write-Host "Echo out all the variables" -ForegroundColor Green            
Write-Host            
Write-Host            
write-host "***********************************************************************" -ForegroundColor Yellow            
$samaccount=$firstsam+$sn7            
echo "Current Login Account is: $samaccount"            
            
$newsam=$firstsam+$newsn7            
echo "New Login Account is: $newsam"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$curemail=$sn7+$firstsam+"@yourdomain.com"             
echo "Current Primary Email is: $curemail"            
            
$newemail=$newsn7+$firstsam+"@yourdomain.com"             
echo "New Primary Email is: $newemail"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$cursecemail=$firstsam+$sn7+"@yourdomain.com"            
echo "Current Secondary Email is: $cursecemail"            
            
$newsecemail=$firstsam+$newsn7+"@yourdomain.com"            
echo "New Secondary Email is: $newsecemail"              
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$newfullname="$firstname" +" " + "$newsur"            
echo "New Name is: $newfullname"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$newdisplayname="$newsur" +"," +" "+ "$firstname"            
echo "New Display Name is: $newdisplayname"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$newprinname="$newsam"+"@yourdomain.com"            
echo "New Principle Name is: $newprinname"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
# Get's the users Home Directory            
$hdir=get-aduser $samaccount -Properties homeDirectory | select -ExpandProperty homeDirectory            
echo "Current Home Directory Path is: $hdir"            
            
# Replace current Home Dir name with new one, which is the new sam account ie #$a = $a.Replace("Scriptign", "Scripting")            
$newhdir=$hdir.Replace("$samaccount", "$newsam")            
echo "New Home Directory Path is: $newhdir"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
$alias=$firstsam+$sn7            
echo "Current Alias is: $alias"            
            
$newalias=$firstsam+$newsn7            
echo "New Alias is: $newalias"            
write-host "***********************************************************************" -ForegroundColor Yellow            
            
Exchange            
}            
            
            
Function Exchange {            
            
pause            
Write-Host "Create and Import PowerShell Session to Exchange Server" -ForegroundColor Green            
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.yourdomain.com/PowerShell/ -Authentication Kerberos            
             
 Import-PSSession $Session -AllowClobber   #By default, Import-PSSession imports all commands except for commands that have the same names as commands in the current session. To import all the commands, use the AllowClobber parameter.            
            
 pause            
 Write-Host "Disable Address Policy for Mailbox" -ForegroundColor Green            
 Set-Mailbox -Identity $alias -EmailAddressPolicyEnabled $false            
            
 pause            
 Write-Host "Remove Current Secondary Email Address" -ForegroundColor Green            
 Set-Mailbox -Identity $alias -EmailAddresses @{Remove="$cursecemail"}            
            
 pause            
 Write-Host "Rename User Object in AD" -ForegroundColor Green            
 get-aduser $samaccount | Rename-ADObject -NewName "$newfullname"            
            
 pause            
 Write-Host "Change AD Attributes like Display Name, SAM Account, UPN, and EMail" -ForegroundColor Green            
 get-aduser $samaccount |Set-ADUser -Surname $newsur -DisplayName "$newdisplayname" -SamAccountName $newsam -UserPrincipalName $newprinname -EmailAddress $newemail            
            
 pause            
 Write-Host "Set New Primary Address and Alias" -ForegroundColor Green            
 Set-Mailbox -Identity $alias -PrimarySmtpAddress $newemail  -Alias $newalias            
            
 pause            
 Write-Host "Enable Address Policy for Mailbox" -ForegroundColor Green            
 Set-Mailbox -Identity $newalias -EmailAddressPolicyEnabled $true            
            
 pause            
 Write-Host "Disable Address Policy for Mailbox" -ForegroundColor Green            
 Set-Mailbox -Identity $newalias -EmailAddressPolicyEnabled $false            
            
 pause            
 Write-Host "Set New Secondary Email Address" -ForegroundColor Green            
 Set-Mailbox -Identity $newalias -EmailAddresses $newsecemail             
            
 pause            
 Write-Host "Remove OLD Primary Email Address- Should not be necessary though. Setting Secondary Add above should clear out all addresses." -ForegroundColor Green            
 Set-Mailbox -Identity $newalias -EmailAddresses @{Remove="$curemail"}            
             
 pause            
 Write-Host "Enable Address Policy for Mailbox" -ForegroundColor Green            
 Set-Mailbox -Identity $newalias -EmailAddressPolicyEnabled $true            
            
 pause            
 Write-Host "Remove PS Session to Exchange" -ForegroundColor Green            
 Get-PSSession | Remove-PSSession            
            
 pause            
 Write-Host "Set Home Directory Folder Path" -ForegroundColor Green            
 Set-ADUser -Identity $newalias -HomeDirectory $newhdir -Verbose            
            
 pause            
 Write-Host "Rename Home Directory" -ForegroundColor Green             
 Rename-Item -Path "$hdir" -NewName $newalias -Force -Verbose            
            
 pause            
 Write-Host "Renamed User Details" -ForegroundColor Green            
 Get-ADUser $newalis -Properties * | FL CN,Company,DisplayName,DistinguishedName,EmailAddress,mailNickname,HomeDirectory,SamAccountName,proxyAddresses,UserPrincipalName            
            
 pause            
            
 Begin            
            
}            
            
# Reference             
#$a = $a.Replace("Scriptign", "Scripting") 
           
# http://exchangeserverpro.com/manually-configuring-email-addresses-for-exchange-server-2013-recipients-using-powershell/   
         
# http://technet.microsoft.com/en-us/library/ee617225.aspx 
           
# http://chinnychukwudozie.com/2013/11/18/renaming-ad-user-object-surname-property/            
# http://dmitrysotnikov.wordpress.com/2010/08/13/manage-email-addresses-without-exchange-cmdlets/  
          
# http://social.technet.microsoft.com/Forums/exchange/en-US/6e005cc5-de5a-4ed8-bb65-fd299e431d65/how-to-removing-x400-addresses-via-powershell?forum=exchangesvradminlegacy      
      
# http://technet.microsoft.com/en-us/library/bb123794(v=exchg.150).aspx            
#$addrs = $mbx.EmailAddresses | Where {$_.Prefixstring -ne "X400"}            
            
               
                 
            
Clear-Host            
            
Begin