Sign in with
Sign up | Sign in
Your question

change accountname using the command line

Last response: in Windows XP
Share
July 19, 2005 11:49:03 PM

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
Anonymous
July 20, 2005 2:53:17 PM

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.m...
!