We help IT Professionals succeed at work.
Get Started

How can I change computername using a script?

Last Modified: 2013-11-29

I am trying to automate a install for Windows Vista, however I am stuck when it comes to changing the name of the computer to the standards we use.

I've used a VB script for this on XP, but when I move this to Vista, I have to change some more strings for it to function properly. It reads the name correct, but after the boot the computername seems to be overwritten.

I've searched the net for solutions to changing it this way and that, but I am unable todo so.

I've also discovered that WMI is disabled during Audit mode, so this has to be done either during the early stages of setup or OOBE. The script I have been trying to use is attached.
' Example VB Script for passing serial number from BIOS to WSName 
' Edit Const's as required 
' Note that some BIOS's may be require the serial number to be trimmed to
' fit within the allows 15 characters, my Toshiba Tecra 8100 for example  
' returns : 70013268,PT810A-12C52,PMAT2A12C522/S3A0305D001
' The sticker on the back just says : 70013268J, as I can determine the 
' manufacturer and model I can trim this down easily enough
' Some BIOS offer the ability to store asset information, I don't have access 
' to one of these for testing but I assume the process will be very similar
' WMI support was introduced with Windows 2000 (?check this) so you'll need to
' use another mechanism to retrieve the serial number on these machines
Option Explicit
Const PATH_TO_WSNAME    = "%systemroot\setup\BKB\WSNAME.EXE"
Dim WSHShell1
Set WSHShell1 = WScript.CreateObject("WScript.Shell")
Dim strSerialNumber, strTEMP, strCommandLine, fso, WshShell
set fso = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
'Check WSName exists
if not fso.FileExists(PATH_TO_WSNAME) then
    WScript.Echo "ERROR, Could not find WSNAME in " & fso.GetParentFolderName(PATH_TO_WSNAME)
end if
' --- Get the serial number, abort script on error
if Not GetWMIStuff("Win32_BIOS","SerialNumber",strSerialNumber) then
    WScript.Echo "ERROR, Could not retrieve serial number"
end if
' --- If we got this far then we have the serial number, if you want to see it
' --- uncomment the next line
' WScript.Echo "The raw serial number is " & strSerialNumber
' --- Tidy up the serial number, if required
strSerialNumber = TidySerialNumber(strSerialNumber)
' --- Uncomment the next line if you want to see it the result
' WScript.Echo "The tidied serial number is " & strSerialNumber
' --- Add leading Alpha characters to avoid DNS confusion
strSerialNumber="VWS-" & strSerialNumber 
' strSerialNumber=strSerialNumber
' WScript.Echo "The tidied serial number is " & strSerialNumber
WSHShell1.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", strSerialNumber
WSHShell1.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\LastComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters\NV Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\ControlSet\Control\ComputerName\ComputerName", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname", strSerialNumber
WSHShell1.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", strSerialNumber
' --- Build Command Line, to check out the command line uncomment
strCommandLine = PATH_TO_WSNAME & " /N:" & strSerialNumber & " " & WSNAME_PARAMETERS
' WScript.Echo "About to run the following command:" & vbCRLF & vbCRLF & strCommandLine
' --- Now call WSName with the serial number
Call WshShell.Run(strCommandLine,1,FALSE)
' --- Game Over!
' ---------------------- Helper Functions ----------------------
Function GetWMIStuff(strInstance,strProperty,strResult)
    Dim colWMIStuff, objWMI, propWMI
    Set colWMIStuff= GetObject("winmgmts:").InstancesOf(strInstance)
    For Each objWMI In colWMIStuff
        For each propWMI in objWMI.Properties_
            If (Not IsNull(propWMI.Value) AND UCase(propWMI.Name) = UCase(strProperty)) Then
                Exit Function
            End If
End Function
' --------------------------------------------------------------
Function TidySerialNumber(strSerialNumber)
    Dim strModel
    On Error Resume Next   ' Turn off error checking
    Call GetWMIStuff("Win32_BIOS","Description",strModel)
    if Instr(UCase(strModel),"TECRA8100") then   ' This will a Tecra8100. I know how to tidy up. 
        TidySerialNumber=Left(strSerialNumber,Instr(strSerialNumber,",")-1) ' Trim at the first comma
    end if  
    On Error Goto 0        ' Turn error checking back on 
End Function

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE