1

My question is about the command choice. I have nested one inside the other because I need to ask one question then if yes add the user to the group and ask the next question add another user. But if the first question is answered with no, I want to ask just the other question add another user.

@echo off
setlocal enabledelayedexpansion
openfiles > NUL 2>&1
if NOT %ERRORLEVEL% EQU 0 goto NotAdmin
GOTO create
:NotAdmin
echo This command prompt is NOT ELEVATED
GOTO END

:create
set /p userName="Please enter a new user name to create:"
net user !userName! > NUL 2>&1
if  %ERRORLEVEL% EQU 0 GOTO ERROR
set /p Passwd="Please enter a password:"
set /p FullName="Please enter user's full name:"
net user !userName! !Passwd! /ADD /FullNAME:"!FullName!" > NUL 2>&1
if %ERRORLEVEL% EQU 0 (
    echo command completed succuessfully
) else (
    echo command did not compelte
)
choice /C yn /M "Would you like to add the user to the local Administrators group"
if ERRORLEVEL 2 (
    choice /C yn /M "Would you like to create another user "
    if ERRORLEVEL 2 (
        GOTO END
    )
    if ERRORLEVEL 1 (
        GOTO create
    )
 )
if ERRORLEVEL 1 (
    net localgroup Administrators !userName! /ADD
)
:ERROR
echo The user !userName! already exist
pause
GOTO create

:END
Mofi
  • 46,139
  • 17
  • 80
  • 143
Marky2019
  • 47
  • 6

2 Answers2

0

Just extracting the choice part. Here I have used 2 methods. One is creating labels ch1 and ch2 where the choice command is told to goto to ch%errorlevel% which in this case errorlevel will either be 1 or 2.

The second has uses the usual if errorlevel 1 but no second condition because if the first condition of errorlevel 1 is not met, it will just fall through to goto end

choice /C yn /M "Would you like to add the user to the local Administrators group"
goto ch%errorlevel%
:ch1
echo add the user to the group here

:ch2
choice /C yn /M "Would you like to create another user "
if errorlevel 1 cls & goto create
goto end
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • okay thank you for your answer i will have to look up most of those things i am very new to this so i havent used ```not defined ``` but i will look it up so i understand your answer completely thanks again – Marky2019 Dec 12 '19 at 23:11