Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having trouble renaming a local computer via WMI.

I'm following the MSDN documentation (and not following it -- in an attempt to get SOME sort of result), but I can't seem to get the Win32_ComputerSystem.Rename() method to work.

Set colComputers = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objComputer in colComputers
  If StrComp(objComputer.Name, Left(objNewComputerName, 15), vbTextCompare) <> 0 Then
    errCode = objComputer.UnjoinDomainOrWorkgroup()
    If errCode <> 0 Then
      MsgBox "Error leaving domain/workgroup. Error: " & errCode
    End If

    errCode = objComputer.Rename(objNewComputerName)
    If errCode <> 0 Then
      MsgBox "Error changing computer name from '" & objComputer.Name & "' to '" & Left(objNewComputerName, 15) & "'. Error: " & errCode
      objComputer.JoinDomainOrWorkgroup domain, password, username

      ' Blindly reboot after attempting to join workgroup/domain
      Set colOs = GetObject("winmgmts:{(Shutdown)}root\cimv2")
      For Each objOs in colOs
    End If
  End If

Whenever I call Rename(), I get an error code 5 (access denied). So maybe I need to leave the domain first? When I call UnjoinDomainOrWorkgroup(), I also get an error code 5.

This is a vanilla Windows 7 installation, with only one account, which is of course, designated as an administrator. I cannot understand why this doesn't work.


share|improve this question
up vote 2 down vote accepted

Have you tried disabling UAC during testing? I'm pretty sure thats why you're getting the Access Denied messages

share|improve this answer
Josh, that seems to have worked, but now I need to "programatically" turn UAC back on before I give this system to the end user. Thoughts? – oo. Dec 2 '09 at 4:53
On second thought, I just need to redesign my provisioning process to somehow kick off a batch of tasks on first boot. Thanks again. – oo. Dec 2 '09 at 4:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.