Search code examples
batch-filepasswordsunlock

Tool to unlock users and reset passwords


I came across a batch script from Spiceworks to unlock users and passwords.

Instead of giving the option to enter the username to unlock or reset the password, I'd like to pre-define a list of users or usernames to unlock and reset the password.

Take for instance the following.

Please choose a user to unlock or reset password:
(1) - John Doe
(2) - Bob smith
(3) - Kelly Brown
(4) - Vicky White


Here is the batch script from Spiceworks with some minor customization.

@echo Welcome to the tool to unlock users and reset passwords
@echo off
echo.
echo.
cd c:\
@echo NOTE: Username is not-case sensitive.
echo.
SET /P user=Please enter a username to unlock or reset password:
echo.

net user %user% /DOMAIN | FIND /I "account active"
echo.
@echo NOTE: The account will either be Locked "No" or active "Yes"
echo.
echo.

set /p userinp="Would you like to unlock this user?(Y/N)" %
IF "%userinp%"=="N" goto 2
IF "%userinp%"=="Y" goto 1 
IF "%userinp%"=="n" goto 2
IF "%userinp%"=="y" goto 1 


:end
echo.
@echo If you recieved errors while using this program
echo.
@echo 1. check the spelling of the username. 
@echo 2. Ensure that the password meets the policy requirements.
echo.
echo.
echo.
Pause
exit

:done
exit

:3
echo.
@echo NOTE: Password must be 12 characters including 1 uppercase letter, 1 special character, alphanumeric characters.
@echo Password is case sensitive
echo.
SET /P Password=Type a new password:
net user %user% %password% /DOMAIN
goto end


:2
set /p userinp="Would you like to reset the user's password?(Y/N)" %
echo.
If "%userinp%"=="N" Goto done
If "%userinp%"=="Y" Goto 3
If "%userinp%"=="n" Goto done
If "%userinp%"=="y" Goto 3


:1
echo.
Net user %user% /domain /active:YES
goto :2

Solution

  • Blend this with the start of what you have...

    @echo off
    setlocal
    set user=
    echo Please choose a user to unlock or reset password:
    echo (1) - John Doe
    echo (2) - Bob smith
    echo (3) - Kelly Brown
    echo (4) - Vicky White
    :loop
    set /p val=
    if %val% equ 1 set user="John Doe"
    if %val% equ 2 set user="Bob Smith"
    if %val% equ 3 set user="Kelly Brown"
    if %val% equ 4 set user="Vicky White"
    if not defined user goto loop
    echo %user%
    

    a slightly more advanced version of this to avoid typing names over and over:

    @echo off
    setlocal enabledelayedexpansion
    rem to add more names, just add the next number in the sequence
    set option1=John Doe
    set option2=Bob Smith
    set option3=Kelly Brown
    set option4=Vicky White
    
    set user=
    echo Please choose a user to unlock or reset password:
    for /L %%a in (1,1,20) do (
        if x!option%%a! neq x (
            echo [%%a] - !option%%a!
            set maxval=%%a
        )
    )
    :loop
    set /p val=
    if x!option%val%! equ x goto loop
    set user="!option%val%!"
    echo %user%