Re: Change local Administrator account name?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 02/14/05


Date: Mon, 14 Feb 2005 12:30:33 +0100

SO wrote:

> Is it possible to change the Administrator account name on a
> remote machine using script?
Hi

The script below should do the job against a remote domain computer
as long as user running the script have administrator rights on it.

If this is for AD domain computers, you could also put it into a
startup script (with a GPO) that runs as part of the boot up process
(before the user logs in). It runs under the system context and has
admin rights.

If you would like to set the password as well, take a look at this
post:
http://groups.google.co.uk/groups?selm=uzxklzP%23EHA.3376%40TK2MSFTNGP12.phx.gbl

'--------------------8<----------------------
'
' Description: Script that renames the builtin administrator
' account to the name set in the variable sNewUser.
'
' Should also work against a remote domain computer as long
' as user running the script have administrator rights on it.
' (you just need to adjust the sComputerName definition)
'

' new user name for the builtin Administrator account
sNewUser = "AdministratorRenamed"

Set oWshNet = CreateObject("WScript.Network")

' get computer name for local computer
sComputerName = oWshNet.ComputerName
' If you want to run the script against a remote computer,
' disable the line above and enable the line below
'sComputerName = "SomeComputer"

' obtain current administrator name regardless of old name
sOldUser = GetAdministratorName(sComputerName)

' Turn off internal error handling
On Error Resume Next

' connect to user object
Set oUser = GetObject("WinNT://" & sComputerName & "/" _
                 & sOldUser & ",user")

If LCase(sNewUser) <> LCase(sOldUser) Then
    Set oComputer = GetObject("WinNT://" & sComputerName)

    ' rename user
    Set oNewUser = oComputer.MoveHere(oUser.ADsPath, sNewUser)
End If

On Error Goto 0

Function GetAdministratorName(sComputerName)
    Dim sUserSID, oWshNetwork, oUserAccount

    Set oUserAccounts = GetObject( _
         "winmgmts:{impersonationLevel=impersonate}!//" _
       & sComputerName & "/root/cimv2").ExecQuery( _
         "Select Name, SID from Win32_UserAccount WHERE Domain = '" _
       & sComputerName & "'")

    On Error Resume Next
    For Each oUserAccount In oUserAccounts
      If Left(oUserAccount.SID, 9) = "S-1-5-21-" And _
         Right(oUserAccount.SID, 4) = "-500" Then
        GetAdministratorName = oUserAccount.Name
        Exit For
      End if
    Next
End Function

'--------------------8<----------------------

-- 
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx


Relevant Pages

  • Re: WMI Script, access denied?
    ... I've got a little script here that I can use to get some information out ... different about accessing a workgroup computer and a domain computer? ... (strComputer, strNamespace, strUser, strPassword) ... W2k3 can only connect to Win9x and NT if credentials supplied. ...
    (microsoft.public.windows.server.scripting)
  • Re: Common tasks of script developer
    ... domain computer, you want your drives mapped, your printers mapped, ... this became the logon script for desktop support. ... accounts created by script are always correct ...
    (microsoft.public.scripting.vbscript)
  • Re: Problem with Domain Computer account
    ... when started by login script. ... does the account have permissions to write a new ... file that Domain computer has full control included file that it copy ...
    (microsoft.public.windows.server.security)
  • Re: Win2K3 Server Group Policy issue
    ... event log on the domain computer that should have run the script? ... I have placed the .exe and its config ...
    (microsoft.public.windows.group_policy)
  • Re: printuientry
    ... you could put it into a logon script. ... The error did not occur as a network administrator. ... I've tried several variations on the command syntax and can not get it ... >> printer is already defined on a "local port", ...
    (microsoft.public.win2000.printing)