PDA

View Full Version : Rename Local User Script


Dave
December 5th 03, 08:01 PM
I am looking to write a script that will rename a
specific local user account that has been setup on
several workstations.
I think this may be more WMI than ADSI but I am new to
scripting.
Does anyone know how to do this? I cannot find anything
on the web, everything I have found is for a domain user
account.
Any help is greatly appreciated.
Thanks

Dave

Torgeir Bakken (MVP)
December 5th 03, 08:03 PM
Dave wrote:

> I am looking to write a script that will rename a
> specific local user account that has been setup on
> several workstations.
> I think this may be more WMI than ADSI but I am new to
> scripting.
> Does anyone know how to do this? I cannot find anything
> on the web, everything I have found is for a domain user
> account.
> Any help is greatly appreciated.

Hi

A tip: There is a better chance to get answers on scripting question in the
group microsoft.public.scripting.wsh

URL to the group for those who uses the not so good Web interface to access the
newsgroups:
http://communities.microsoft.com/Newsgroups/default.asp?ICP=MSCOM&sLCID=US&newsgroup=microsoft.public.scripting.wsh

For ADSI scripting, the group microsoft.public.adsi.general is also a good one.

URL to the group for those who uses the not so good Web interface to access the
newsgroups:
http://communities.microsoft.com/Newsgroups/default.asp?ICP=MSCOM&sLCID=US&newsgroup=microsoft.public.adsi.general



For WinXP, you can use both WMI and ADSI to rename local users (for Win2k, WMI
is not an option). Note that you can do much more with local user accounts
using ADSI than with WMI. Code for both methods below.


=============================================
WMI: Win32_UserAccount.Rename
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/rename_method_in_class_win32_useraccount.asp



' code start

sOldUser = "Administrator"
sNewUser = "RenamedAdmin"

sComputerName = "." ' use "." for local computer

Set colUsers = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sComputerName & "\root\cimv2").ExecQuery _
("select Name from Win32_UserAccount where name = '" & sOldUser & "'")

If colUsers.Count > 0 Then
For Each oUser In colUsers
iRC = oUser.Rename(sNewUser)
If iRC = 0 Then
WScript.Echo "User renamed"
Else
WScript.Echo "Rename method returned ERROR # " & iRC
End if
Next
Else
WScript.Echo "No user found"
End If



=============================================
Script below renames local users on a computer (local or remote), using ADSI
from a vbscript.


' code start

sOldUser = "Administrator"
sNewUser = "RenamedAdmin"


sComputerName = "SomeComputer"

' If you want to do this on the local computer, enable the following lines
' to get computer name for local computer
'Set oWshNet = CreateObject("WScript.Network")
'sComputerName = oWshNet.ComputerName

sNewUser = "RenamedAdmin"
sOldUser = "Administrator"

Set oComputer = GetObject("WinNT://" & sComputerName)

' Turn off error handling in case sOldUser does not exist
On Error Resume Next
Set oUser = GetObject("WinNT://" & sComputerName & "/" & sOldUser & ",user")
Set oNewUser = oComputer.MoveHere(oUser.ADsPath, sNewUser)
On Error Goto 0



--
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

Dave
December 5th 03, 08:05 PM
Thanks!!
Tried it and it worked!
This should save us a bundle of time!!



>-----Original Message-----
>Dave wrote:
>
>> I am looking to write a script that will rename a
>> specific local user account that has been setup on
>> several workstations.
>> I think this may be more WMI than ADSI but I am new to
>> scripting.
>> Does anyone know how to do this? I cannot find anything
>> on the web, everything I have found is for a domain
user
>> account.
>> Any help is greatly appreciated.
>
>Hi
>
>A tip: There is a better chance to get answers on
scripting question in the
>group microsoft.public.scripting.wsh
>
>URL to the group for those who uses the not so good Web
interface to access the
>newsgroups:
>http://communities.microsoft.com/Newsgroups/default.asp?
ICP=MSCOM&sLCID=US&newsgroup=microsoft.public.scripting.ws
h
>
>For ADSI scripting, the group
microsoft.public.adsi.general is also a good one.
>
>URL to the group for those who uses the not so good Web
interface to access the
>newsgroups:
>http://communities.microsoft.com/Newsgroups/default.asp?
ICP=MSCOM&sLCID=US&newsgroup=microsoft.public.adsi.general
>
>
>
>For WinXP, you can use both WMI and ADSI to rename local
users (for Win2k, WMI
>is not an option). Note that you can do much more with
local user accounts
>using ADSI than with WMI. Code for both methods below.
>
>
>=============================================
>WMI: Win32_UserAccount.Rename
>http://msdn.microsoft.com/library/en-
us/wmisdk/wmi/rename_method_in_class_win32_useraccount.asp
>
>
>
>' code start
>
>sOldUser = "Administrator"
>sNewUser = "RenamedAdmin"
>
>sComputerName = "." ' use "." for local computer
>
>Set colUsers = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" _
> & sComputerName & "\root\cimv2").ExecQuery _
> ("select Name from Win32_UserAccount where name = '"
& sOldUser & "'")
>
>If colUsers.Count > 0 Then
> For Each oUser In colUsers
> iRC = oUser.Rename(sNewUser)
> If iRC = 0 Then
> WScript.Echo "User renamed"
> Else
> WScript.Echo "Rename method returned ERROR # " &
iRC
> End if
> Next
>Else
> WScript.Echo "No user found"
>End If
>
>
>
>=============================================
>Script below renames local users on a computer (local or
remote), using ADSI
>from a vbscript.
>
>
>' code start
>
>sOldUser = "Administrator"
>sNewUser = "RenamedAdmin"
>
>
>sComputerName = "SomeComputer"
>
>' If you want to do this on the local computer, enable
the following lines
>' to get computer name for local computer
>'Set oWshNet = CreateObject("WScript.Network")
>'sComputerName = oWshNet.ComputerName
>
>sNewUser = "RenamedAdmin"
>sOldUser = "Administrator"
>
>Set oComputer = GetObject("WinNT://" & sComputerName)
>
>' Turn off error handling in case sOldUser does not exist
>On Error Resume Next
>Set oUser = GetObject("WinNT://" & sComputerName & "/" &
sOldUser & ",user")
>Set oNewUser = oComputer.MoveHere(oUser.ADsPath,
sNewUser)
>On Error Goto 0
>
>
>
>--
>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
>
>
>.
>

Google