creating a random password in batch

I am trying to create a random password using batch.

I googled and found a method but it involves setlocal delayed exapansion and all which is affecting my existing code (with special characters and stuff)

This is the code I got:

@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!

However I dont know how to store the result into a variable in say x so that I can get it as %x%

Like x=!_RndAlphaNum!

echo %x% can show me the random number generated.

unrelated question:

Is there a way to generate a random alpha-num string without using setlocal enabledelayedexpansion.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
Add a SET command

SET X=!_RndAlphaNum!

Then use %X%, i.e.
Michael PfisterCommented:
I'd say you have to use enabledelayedexpansion for the random password.
But if you don' t like it issue a

Setlocal DisableDelayedExpansion

after generating the password
And you can use the original password variable without assigning it to another variable

Setlocal DisableDelayedExpansion
ECHO %_RndAlphaNum%
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The above recommendation does not persist outside of the batch file. Each setlocal starts a new set of (changed) variables, and changes will be reverted after endlocal or batch end.
One of the ways to allow for keeping results it by "exporting":
endlocal & set _RndAlphaNum=%_RndAlphaNum%

Open in new window

as last line should work - because the complete line is parsed as a whole, %var% replacement takes place (inserting the string value instead of the var), and then the line is executed.
Steve KnightIT ConsultancyCommented:
If you could use a few lines of VBScript embedded in your batch file try this.  Put any characters you want to use in the choice="" line and change for I=1 to 8 to a different number if you want longer.


@echo off
(echo choice="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"
echo password=""
echo Randomize
echo for i=1 to 8: password=password ^& mid^(choice,int^(rnd*len^(choice^)^)+1,1^) : next
echo wscript.echo password
) > "%temp%\make_password.vbs"

set password=
for /f %%a in ('cscript //nologo "%temp%\make_password.vbs"') do set password=%%a
echo Password this time is: %password%
goto start

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.