change accountname using the command line

Robert

Distinguished
Apr 1, 2004
811
1
18,980
Archived from groups: microsoft.public.windowsxp.general (More info?)

Hi,

does anybody know how i can change the name of an useraccount using the
command line like "net user /change old-name new-name"?

Thanks
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.general (More info?)

Robert wrote:

> Hi,
>
> does anybody know how i can change the name of an useraccount
> using the command line like "net user /change old-name new-name"?
Hi,

You could put the VBScript you find further below in this post into a
..vbs file, e.g. named renameuser.vbs. The script supports input
arguments, so you run it from a command line with
"cscript.exe renameuser.vbs old-name new-name".

Here is the output on my computer when I run it from a command line:

C:\>cscript.exe //nologo c:\scripts\renameuser.vbs user1 user2
User account successfully renamed from user1 to user2

If the script is able to rename the account, the errorlevel
returned is 0, if it is not, errorlevel 1 will be returned.


'--------------------8<----------------------
'
' Description:
' Script that renames a local user account
' Will return errorlevel 1 if it fails

Set oArgs = Wscript.Arguments
If oArgs.Count <> 2 Then
WScript.Echo "Invalid input, needs to be: " & vbCrLf & vbCrLf _
& WScript.ScriptName & " <old-name> <new-name>"
WScript.Quit 1 ' return with errorlevel 1
End If

Set oWshNet = CreateObject("WScript.Network")

' get computer name for local computer
sComputerName = oWshNet.ComputerName

' Turn off internal error handling
On Error Resume Next

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

If Err.Number = 0 Then
Set oComputer = GetObject("WinNT://" & sComputerName)

' rename user
Set oNewUser = oComputer.MoveHere(oUser.ADsPath, oArgs(1))

If Err.Number = 0 Then
WScript.Echo "User account successfully renamed from " _
& oArgs(0) & " to " & oArgs(1)
Else
WScript.Echo "Was not able to rename user account from " _
& oArgs(0) & " to " & oArgs(1)
WScript.Quit 1 ' return with errorlevel 1
End If

Else
WScript.Echo "User account " & oArgs(0) & " does not exist!"
WScript.Quit 1 ' return with errorlevel 1
End If

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


WSH 5.6 documentation (local help file) can be downloaded from here
if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp



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