0

I am using batch scripting to do basic user functions (add user, delete, change account type etc.) rather than manually do it all. The program asks the user what they would like to do, following options with A, B, C, … G. This part works fine, however, after the user enters in the option, it imminently closes. After checking, the commands following the if statement do not run. How should I fix the if statements?

@echo off

net user

echo What would you like to do?

echo A: Turn off default admin account
echo B: Turn off default guest account
echo C: Add a user
echo D: Remove a user
echo E: Change a users privilages
echo F: Change a password
echo G: Nothing

set /p en = "Enter in the Letter:"

if %en% == "A" (
    net user administrator /active:no
    echo Built-In Admin Is Turned Off
    Pause
)

if %en% == "B" (
    net user guest /active:no
    echo Guest Account Is Turned Off
    Pause
)

if %en% == "C" (
    set /p adduser = What is the name of the user you would like to add(Case Sensitive)?
    set /p addpassword = What is the password you would like to add to %adduser%?
    net user %adduser% %addpassword% /added
    echo Added %adduser%
    set /p objUser = GetObject(WinNT://MyComputer/%adduser%,user)
    %objUser%.PasswordExpired = 1
    %objUser%.SetInfo
    echo Password Will Need To Be Changed On Next Logon
    Pause

    if %en% == "D" (
    set /p deluser = What is the name of the user you would like to delete(Case Sensitive)?
    net user %deluser% /del
    echo User %deluser% has been Deleted

The code should be able to run the commands corresponding with the letter, and then stay open until the user is ready to close it. However, it will not run the sequence and closes upon the user entering in a letter to set for variable 'en'.

If it is not out of inconvenience, please explain why your code works in simpler terms. I am fairly new to using batch scripting and still do not understand a vast majority of commands and terms.

JENA GARZA
  • 11
  • 2
  • The string comparison is literal. If quotes are on one side they must be on the other side. You also need to remove the spaces before the equals symbol in the set command – Squashman Jan 11 '19 at 04:07
  • 4
    Possible duplicate of [Declaring and using a variable in Windows batch file (.BAT)](https://stackoverflow.com/questions/10552812/declaring-and-using-a-variable-in-windows-batch-file-bat) – double-beep Jan 11 '19 at 06:15
  • 1
    I suggest you to use the choice command instead of set /p. Also, you should take a look at https://stackoverflow.com/questions/26386697/why-is-no-string-output-with-echo-var-after-using-set-var-text-on-comman/26388460#26388460 which is an another duplicate of this question. – double-beep Jan 11 '19 at 06:18

1 Answers1

1

The final code should look like this:

@echo off

net user

echo What would you like to do?

echo A: Turn off default admin account
echo B: Turn off default guest account
echo C: Add a user
echo D: Remove a user
echo E: Change a users privilages
echo F: Change a password
echo G: Nothing

set /p en="Enter in the Letter:"
choice /c:ABCDEFG /M "Enter in the letter:" /N

if errorlevel 7 (
    rem This means the letter is 'G'; add some code here:
)

if errorlevel 6 (
    rem This means the letter is 'F'; add some code here:
)

if errorlevel 5 (
    rem This means the letter is 'E'; add some code here:
)

if errorlevel 4 (
    set /p "deluser=What is the name of the user you would like to delete (Case Sensitive)? "
    net user %deluser% /del
    echo User %deluser% has been Deleted
    pause
)

if errorlevel 3 (
    set /p "adduser=What is the name of the user you would like to add (Case Sensitive)? "
    set /p "addpassword=What is the password you would like to add to %adduser%? "
    net user %adduser% %addpassword% /added
    echo Added %adduser%
    set /p "objUser=GetObject(WinNT://MyComputer/%adduser%,user) "
    %objUser%.PasswordExpired = 1
    %objUser%.SetInfo
    echo Password Will Need To Be Changed On Next Logon
    pause
)

if errorlevel 2 (
    net user guest /active:no
    echo Guest Account Is Turned Off
    Pause
)

if errorlevel 1 (
    net user administrator /active:no
    echo Built-In Admin Is Turned Off
    Pause
)

while all things fixed were mentioned in comments. Type if /?, set /? and choice /? for more information in cmd.

double-beep
  • 5,031
  • 17
  • 33
  • 41