Zum Inhalt

Powershell – Rename domain computer remotely (within an active directory domain as well)

I wanted to rename a couple of computers within our active directory. After some research i figured out thats it seems to be nearly impossible to rename a computer by just touching one object: the ad computer object, or the computer (client) itself. My first thought was „ok, you have to rename both objects, rejoin the computer and hope everything works“. But that’s no solution, that makes me happy, because the more steps you do, the more problems can occure. For examples, what happens if the computer has to reboot, after renaming to get correct rejoined? Do i have to create a local admin account at the clientside to have permissions after the computer lost his connection to ad? And so on…
I played around, renamed the ad object, rebooted it – negative, the computer has to be joined again. After that i tried it the „bottom up“ way by renaming the computer by hand, and rebooted it instantly. While the computer was shutting down i noticed, that the computerobject in active directory was renamed before the computer was finished with its shutdown process.
So i tried this several times and every time the ad computerobject was renamed properly. YAY!
I had my solution. It can’t get more easy to rename a computer without rejoining it.

After this conclusion i tried to do it remotly with powershell, so i googled and found some sites about using netdom.exe. But, yep, right, calling a remote program was not the way i want to solve this problem :). I found some information on using wmi that suits me, so i started writing a powershell script to test it. As expected it’s a bit complicated to rename a computer within a domain by wmi. You have to overcome 3 „hurdles“:
– Use Authentication
– Using username and passwort of an administrative account
– Reboot the computer instantly after renaming

Here is a script, that does everything you want. Rename the computer and reboot it if renaming was successfull. Look at the variable section at the script header and fill in your administrative credentials (~domain admin)

Due to some issues with copy and paste of this sourcecode, there is a download (zip) available here -> Rename Computer Powershell Script.
After you have downloaded and extracted the zip file, edit the script with your favorite editor (I always use notepad++ or notepad). At the beginning of the script, there is a section called variable. You have to enter your Active Directory username (with ad forrest) and the corresponding password.
Now start your Powershell and go to the directory where your script is and start it with the following command:

There is no check on the computername yet, so every computername you submit is more or less valid.
More information about the limitations of a computername can be found here: http://support.microsoft.com/kb/909264/en-us

Feel free to use this script and leave me a comment!

Published inCodingPowershellScriptingWMI


  1. Seb Seb

    You can actually download the script @ this address : http://dl.free.fr/hLG3mzgkY

    You just have to input Domain + Account + Password (Delegation or admin rights for this account have to exist on your AD/needed OUs to alter computer objects‘ name) in the script.
    [And perhaps changing the name of the TS variable („$TSEnv:NewOSDComputerName“) in order to match yours…]

    In the TS, please respect these steps:
    -1 : Install WMF 3.0 update on the freshly deployed and running OS + Restart Computer,
    -2 : Set PSExecutionPolicy to a lower level,
    -3 : Run the Script + Restart Computer,
    -4 : Set PSExecutionPolicy back to Default.

    That’s all ! =)

    Best regards,

  2. shawn shawn

    Hi, Marco

    i try to use your script to rename my domain computer, it succeed and very helpful to me, and now, i need to bulk rename my domain computers, is anyway to rename computers with csv files ?

  3. mmmh…
    why not…

    $computers = Get-adcomputer | where {$_.name –like “*”}
    $num = 0
    Foreach($computer in $computers)
    For($num=1;$num –lt $computers.count;$num++)
    echo Rename-computer –computername $computer –newname “s-$num” –domaincredential domain\user –force –restart

  4. True, but the computer rename function was implemented in Powershell v2 or v3. At the time I worte this post, this was not available

  5. Duke Duke

    The script works great. Do you have any suggestion on how you would prompt the user executing the script to provide SmartCard and PIN credentials when the script is run? This assumes the SmartCard is currently in use by the person running the script..

    Any help would be greatly appreciated


  6. David David

    Thank you for your great effort! Unfortunatly i always get the following „access denied“ error. I tried different accounts which should have the right privileges. Can someone help?

    Zugriff verweigert (Ausnahme von HRESULT: 0x80070005 (E_ACCESSDENIED))

    best regards,


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.