Batch file for creating new user account

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
soulesswanderer
Posts: 1
Joined: 28 Feb 2015 12:54

Batch file for creating new user account

#1 Post by soulesswanderer » 28 Feb 2015 13:00

I need to create a batch file using the "net user" command. I'm familiar with creating accounts using the "net user" command, but I need to create a batch file that asks the operator to enter the user name to be created, then run the command using the "net user" interface.

I'm more or less familiar with the commands needed to do this, I'm just not exactly sure how to order them all and execute them in the instance of using the "net user" interface.

Any tips would be great, I'm not asking for someone to do my dirty work and write the batch file for me, I'm just asking for someone to point me in the right direction.

Any help or tips would be great.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file for creating new user account

#2 Post by Squashman » 28 Feb 2015 19:39

Since you know how to use the NET command all you need then is a prompt to enter in the username and password. You can use the SET /P command for that.

Code: Select all

SET /P user=Please Enter the Username:
SET /P password=Please Enter the Password:
net user %user% %password% /add

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch file for creating new user account

#3 Post by Compo » 02 Mar 2015 08:04

You could even forget about using net.exe and go for a powershell script, (suggested for no other reason but to mask the input password).

Example with no built in checking/safety for the input

Code: Select all

$desc = 'local admin account created by interactive script'
$username = Read-Host -Prompt "Enter a User Name"
$password = Read-Host -Prompt "Enter a password for $username" -AsSecureString
$decodedpassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$computername = [System.Environment]::MachineName
$computer = [ADSI]"WinNT://$computername,computer"
$user = $computer.Create("user", $username)
$user.SetPassword($decodedpassword)
$user.SetInfo()
$user.description = $desc
$user.SetInfo()
$user.UserFlags = 65536
$user.SetInfo()
$group = [ADSI]("WinNT://$computername/administrators,group")
$group.add("WinNT://$username,user")

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file for creating new user account

#4 Post by Squashman » 02 Mar 2015 09:00

Compo wrote:You could even forget about using net.exe and go for a powershell script, (suggested for no other reason but to mask the input password).

I believe we have a way to mask the password in batch as well. Would need to search for the thread but I had mentioned that we should add it to the main website's function library.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file for creating new user account

#5 Post by ShadowThief » 02 Mar 2015 10:34

We do and it's beautiful.

Code: Select all

@echo off
    setlocal enableextensions disabledelayedexpansion

rem Call the subroutine to get the password   
    call :getPassword password

rem Echo what the function returns
    if defined password (
        echo You have typed [%password%]
    ) else (
        echo You have typed nothing
    )

rem End of the process   
    endlocal
    exit /b


rem Subroutine to get the password
:getPassword returnVar
    setlocal enableextensions disabledelayedexpansion
    set "_password="

    rem We need a backspace to handle character removal
    for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"

    rem Prompt the user
    set /p "=password ?:" <nul

:keyLoop
    rem retrieve a keypress
    set "key="
    for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
    set "key=%key:~-1%"

    rem handle the keypress
    rem     if No keypress (enter), then exit
    rem     if backspace, remove character from password and console
    rem     else add character to password and go ask for next one
    if defined key (
        if "%key%"=="%BS%" (
            if defined _password (
                set "_password=%_password:~0,-1%"
                setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
            )
        ) else (
            set "_password=%_password%%key%"
            rem remove the * for UNIX-style password masking
            set /p "=*"<nul
        )
        goto :keyLoop
    )
    echo(
    rem return password to caller
    if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
    endlocal & set "%~1=%_password%" & exit /b %exitCode%

viewtopic.php?p=33538#p33538

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file for creating new user account

#6 Post by Squashman » 02 Mar 2015 10:39

Thanks,
And I think Carlos' script has been improved over the last few years as well.

Post Reply