I suggest the following batch file code for this task:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
%SystemRoot%\System32\openfiles.exe >NUL 2>&1
if errorlevel 1 goto NotAdmin
goto Create
:NotAdmin
echo This command prompt is NOT ELEVATED.
goto END
:Create
set "NameUser="
set /P "NameUser=Please enter a new user name to create: "
if not defined NameUser goto Create
set "NameUser=!NameUser:"=!"
if not defined NameUser goto Create
%SystemRoot%\System32\net.exe user "!NameUser!" >NUL 2>&1
if not errorlevel 1 (
echo The user !NameUser! exists already.
pause
goto Create
)
set /P "Passwd=Please enter a password: "
set /P "FullName=Please enter user's full name: "
%SystemRoot%\System32\net.exe user "!NameUser!" "!Passwd!" /ADD /FullNAME:"!FullName!" >NUL 2>&1
if errorlevel 1 (
echo Creation of user account failed.
) else (
echo Creation of user account completed succuessfully.
)
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to add the user to the local administrators group? [Y/N]"
if errorlevel 2 %SystemRoot%\System32\net.exe localgroup Administrators "!NameUser!" /ADD
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to create another user? [Y/N]"
if errorlevel 2 goto Create
:END
endlocal
The environment variable USERNAME
is a predefined Windows environment variable as it can be seen on opening a command prompt and running set user
which lists all environment variables with their values starting case-insensitive with the string user
. See also the Wikipedia article about Windows environment variables. Therefore the variable NameUser
is used in the batch file instead of userName
.
The help for command IF output on running if /?
in a command prompt window explains the recommended syntax to evaluate ERRORLEVEL
holding exit code of previously executed program. The answer on single line with multiple commands using Windows batch file explains this syntax in detail on examples. On all operating systems it is highly recommended that no executable exits ever with a negative value. The executables used here and written by Microsoft for Windows exit never with a negative value.
All executables in above batch code are referenced with full qualified file name to make the batch file independent on current value of local environment variables PATH
and PATHEXT
. So Windows command processor must not search for the files to execute.
The optimization is quite simple by exchanging the letters Y
and N
in the list of choices. The help output on running choice /?
in a command prompt window explains that choice
exits with 1
if the user presses the key of first character in choices list and with 2
if the user presses the key of second character in choices list. Now with user pressing key Y the just created user account is added to local administrator group and then the user is prompted for creating one more account like on pressing key N.
The second question using command choice
is also specified with exchanged Y
and N
to make it again possible with a single IF condition to jump to code below label Create
on user pressing key Y while otherwise continue with exiting the batch file processing after restoring explicit previous local environment.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
choice /?
echo /?
endlocal /?
goto /?
if /?
net /?
net localgroup /?
net user /?
openfiles /?
pause /?
set /?
setlocal /?