Using a Script or TS to Rename a Computer

A while back I had a customer that had to do a computer rename project to change some systems with their previous naming standard to their new naming standard. They wanted to do this without requiring someone to log on to each system. Yesterday I saw a Reddit thread asking how to rename a computer during a TS while keeping the same AD object and it occurred to me that the same general technique would work,

The key to the technique is allowing the computer access to rename itself in AD then using a script to carry out the rename operation.

Identity the OU where you want to grant machines the ability to rename themselves. You can even make an OU that you move systems in to until they are renamed, then move them back out.

Right mouse click and go to Delegate Control

In the wizard that comes up, click “Next”, then enter “SELF” as the object name.

Click “Next”, then “Create a custom task to delegate”

Choose the “Only the following object in the folder” radio button, then “Computer objects” from the list.

Click “Next” then select “Write” from the list. Note: this will check various write options throughout the list.

You can now click “Next” then “Finish”

I created a package program that was the cmd line of “cmd /c” and a mandatory reboot, then deployed to the systems as optional. Now you are ready to create a script in the script node, just plug in the info for your package so the system can trigger it to force the reboot. It is a cheap workaround, but it keeps the reboot interface standard for any user logged in.

For a TS, you can omit the package portion of the script and have a restart computer step after the script. You’ll also have to feed the script the desired name somehow. You could expand this script to prompt the user, or use one of the already existing UDI options out there.

Param([parameter(Position = 0)][string]$NewName)

$PC = Get-WmiObject -Class Win32_ComputerSystem
$PC.Rename($NewName) | out-null

$Currentname = Get-ItemPropertyValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" -Name "ComputerName"
$Pendingname = Get-ItemPropertyValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" -Name "ComputerName"
if ($Currentname -eq $Pendingname) { write-output "Computer name change failed"; EXIT 1 }
else {
    ([wmiclass]'ROOT\ccm\ClientSdk:CCM_ProgramsManager').ExecuteProgram("reboot system", "CM2000F3") | out-null
    Start-Sleep 10
    $rebootPending = (([wmiclass]'ROOT\ccm\ClientSDK:CCM_ClientUtilities').DetermineIfRebootPending()).RebootPending
    if ($rebootPending) { Write-output "Computer name changed, reboot pending." }
    else { Write-output "Computer name changed, reboot NOT triggered."; EXIT 2 }