Your code does not work due to an error in this line:
ComputerName = obj.shell.RegRead(regComputerName)
Instead of obj.shell, you should reference objShell. It should look like this:
Set objShell = WScript.CreateObject("WScript.Shell") strRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername" strComputerName = objShell.RegRead(strRegKey) WScript.Echo strComputerName
However, there are much more reliable ways to get a computer name without having to deal with the registry.
From WSH (as suggested above)
Set WshNetwork = WScript.CreateObject("WScript.Network") strComputerName = WshNetwork.ComputerName WScript.Echo "Computer Name: " & strComputerName
From the environment variable ...
Set wshShell = WScript.CreateObject("WScript.Shell") strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") WScript.Echo "Computer Name: " & strComputerName
From WMI ...
strcomputer = "." Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMISvc.ExecQuery("Select * from Win32_ComputerSystem",, 48) For Each objItem in colItems strComputerName = objItem.Name WScript.Echo "Computer Name: " & strComputerName Next
From ADSI ...
Set objSysInfo = CreateObject("WinNTSystemInfo") strComputerName = objSysInfo.ComputerName WScript.Echo "Computer Name: " & strComputerName
From ADSI (only works for domain members) ...
Set objSysInfo = CreateObject("ADSystemInfo") strComputerName = objSysInfo.ComputerName WScript.Echo "Computer Name: " & strComputerName
... and the last way for users of Windows XP ...
Set objPC = CreateObject("Shell.LocalMachine") strComputerName = objPC.MachineName WScript.Echo "Computer Name: " & strComputerName