Rename computer and join to domain in one step with PowerShell

This solution is working:

  • Enter the computer in the Active Directory domain with authentication (no Restart)
  • Rename the computer with authentication (no Restart)
  • after, Restart

In code:

# get the credential 
$cred = get-credential

# enter the computer in the right place
Add-Computer -DomainName EPFL -Credential $cred -OUPath "...,DC=epfl,DC=ch"

# rename the computer with credential (because we are in the domain)
$Computer = Get-WmiObject Win32_ComputerSystem
$r = $Computer.Rename("NewComputerName", $cred.GetNetworkCredential().Password, $cred.Username)

There are actually several reasons that you have to reboot after renaming a computer, or when joining a domain (which is basically the same operation with validation by AD). One being that on NT based computers (I believe this started with Windows 2000), the Application and Network services read the computer name when they are started. Which is the only time they read the computer name, so if you were to rename the computer without a restart, the network and application services would not respond to the new computer name. This particularly becomes important when you are first renaming the computer, and then trying to join a domain, as the kerberos handshake can not be completed without the network stack responding to the correct computer name.

Another reason is that several registry keys make use of the computer name, and those keys cannot be changed while they are loaded into memory (this is incidentally also why some programs require a reboot to complete installation or uninstallation).

You could use the RunOnce registry key (msdn.microsoft.com/en-us/library/aa376977%28v=vs.85%29.aspx) to run your domain join script automatically upon reboot, but you're still going to have to reboot for both operations.

If you really wanted to get tricky, you could add some code to your rename script that would set the RunOnce registry key to launch the domain join script upon reboot. Be aware though if you are going to do this, that the script that will be writing to the HKLM hive must be run as an administrator (especially important if you have UAC turned on).

If you want to do that, you'd use something like this at the end of your Rename-Computer function:

Set-Location -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce'
Set-ItemProperty -Path . -Name joinDomain -Value "C:\scripts\joinDomain.ps1"
Restart-Computer

This will create a subkey in the RunOnce registry key (assuming you are running Vista/7/2008) named "joinDomain" with the value of "C:\scripts\joinDomain.ps1"

If that doesn't work for you, try changing the second line to this:

Set-ItemProperty -Path . -Name joinDomain -Value 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe "C:\scripts\joinDomain.ps1"'

Let me know if you have troubles.


You can just use Add-Computer, there is a parameter for "-NewName"

Example: Add-Computer -DomainName MYLAB.Local -ComputerName TARGETCOMPUTER -newname NewTARGETCOMPUTER

You might want to check also the parameter "-OPTIONS"

http://technet.microsoft.com/en-us/library/hh849798.aspx