@echo off 
setlocal EnableDelayedExpansion

:: Define the characters that a password can be created with
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"

:: Prompt user to enter their wanted password length
set /p length=What password length would you like:

:: Check if the user has entered a length. If not, run block of code
if not defined length (
	echo No desired password length entered.
	pause
	exit /b
)

:: Validate that the users input is a number
set /a test=%length% 2>nul
if "%test%" neq "%length%" (
	echo Invalid number. Enter a valid number.
	pause 
	exit /b
)

:: Create the empty passowrd variable
set "password="

:: For loop from 1 to the users wanted password length
for /L %%i in (1,1,%length%) do (
	set /a index=!random! %% 72
	:: Take one character from 'chars' based on the random index
	for %%j in (!index!) do set "char=!chars:~%%j,1!"
	:: Set the randomly generated character to the passowrd
	set "password=!password!!char!"
)

echo.
:: Output the randomly generated passowrd to the user
echo Your randomly generated password is: !password!
echo.
pause
