Rename computer (VBScript)

This code can be found in Chapter 2 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' This code changes the name of a computer.  It does NOT modify 
' the computer's account in the domain if one exists.
' ---------------------------------------------------------------
' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strComputer = "<CurrentServerName>"
strNewName = "<NewServerName>"
' ------ END CONFIGURATION ---------
const HKLM   = &H80000002
strKeyPath   = "System\CurrentControlSet\Control\ComputerName\ComputerName"
set objReg = GetObject("winmgmts:\\" & strComputer & _
                       "\root\default:StdRegProv")
intRC = objReg.SetStringValue(HKLM, strKeyPath, "ComputerName", strNewName)
if intRC <> 0 then
   WScript.Echo "Error setting ComputerName value: " & intRC
else
   WScript.Echo "Successfully set ComputerName value to " & strNewName
end if

strKeyPath   = "System\CurrentControlSet\Services\Tcpip\Parameters"
intRC = objReg.SetStringValue(HKLM, strKeyPath, "NV Hostname", strNewName)
if intRC <> 0 then
   WScript.Echo "Error setting NV Hostname value: " & intRC
else
   WScript.Echo "Successfully set NV Hostname value to " & strNewName
end if

WScript.Echo "Rebooting system..."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
for each objOS in objWMI.InstancesOf("Win32_OperatingSystem")
   objOS.Reboot()
next

This code has been viewed 3203 times.

New from the creators of TechTasks.com: StatSheet.com