Account Rename Tool.txt - Notepad

Account Rename Tool

The below PowerShell script utilises the Quest AD Module to rename AD user accounts and their home drive folders. It then emails the output.
#Quest Account Rename Tool - Author Nick Beare
#Input file format (csv), headings below:
#old_username,new_username,lastname,firstname
#The tool does the following:
#On the AD User Object it renames DN, CN, SAMAccountName, Firstname, Lastname, Initials, HomeDirectory and ProfilePath.
#On the file server it renames the user's actual folder.
Add-PSSnapin Quest.ActiveRoles.ADManagement
$date = get-date -format ddMMyyyy
$file = $date + "_rename.txt"
start-transcript -path \\hostname\d$\Account_Renames\$file
$peeps = import-csv -path \\hostname\d$\Account_Renames\users.csv
foreach ($peep in $peeps)
{
$upn = $peep.new_username.tolower() + "@bhp.com.au"
$sid = get-qaduser -identity $peep.old_username -includedproperties sid | select-object -expandproperty sid | select-Object -ExpandProperty value
$profilepath = get-qaduser -identity $peep.old_username -includedproperties profilepath | select-object -expandproperty profilepath
$homedirectory = get-qaduser -identity $peep.old_username -includedproperties HomeDirectory | select-object -expandproperty homedirectory
$newprofilepath = $profilepath -replace $peep.old_username, $peep.new_username.tolower()
$newhomedirectory = $homedirectory -replace $peep.old_username, $peep.new_username.tolower()
rename-item -path $homedirectory -newname $newhomedirectory
rename-qadobject -identity $peep.old_username -newname $peep.new_username.tolower()
set-qaduser -identity $sid -firstname ((Get-Culture).textinfo.totitlecase($peep.firstname)) -lastname ((Get-Culture).textinfo.totitlecase($peep.lastname)) -samaccountname $peep.new_username.tolower() -initials (($peep.firstname.substring(0,1) + $peep.lastname.substring(0,1)).toupper()) -HomeDirectory $newhomedirectory -profilepath $newprofilepath -UserPrincipalName $upn
}
stop-transcript
Send-MailMessage -To me@mail.com -From me@mail.com -Subject "Weekly Rename Report" -SmtpServer mail.com -Attachments \\hostname\d$\Account_Renames\$file -Body "Hi, technical rename report attached."

Back