Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Created December 4, 2012 19:49
Show Gist options
  • Save davidruhmann/4207960 to your computer and use it in GitHub Desktop.
Save davidruhmann/4207960 to your computer and use it in GitHub Desktop.
[Batch] Generate a GUID in a command line batch
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:AppendHex <xReturn> <xInput>
:: Append a hexidecimal number to the end of the input.
:: 1. Generate Random Number = 0-15
:: 2. Convert Number to Hexidecimal
:: 3. Append to Input
setlocal
set /a "xValue=%RANDOM% %% 16"
if "%xValue%"=="10" set "xValue=A"
if "%xValue%"=="11" set "xValue=B"
if "%xValue%"=="12" set "xValue=C"
if "%xValue%"=="13" set "xValue=D"
if "%xValue%"=="14" set "xValue=E"
if "%xValue%"=="15" set "xValue=F"
endlocal & if not "%~1"=="" set "%~1=%~2%xValue%"
goto :eof
:: by David Ruhmann
:: GUID
:: Generate a 32 Character Hexidecimal String
:: Example: 97F00720-652D-563D-CE88-175DB095E7B5
:: Valid Characters: 0123456789ABCDEF
:: by David Ruhmann
:: Hide Commands
@echo off
:: Example Usage
call :GUID GUID
call :GUID2 GUID2
call :GUID3 GUID3
:: Display GUID
echo.%GUID%
echo.%GUID2%
echo.%GUID3%
pause
goto Exit
:Exit
:: Return Value
exit /b 0
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GUID <xReturn>
:: Generate a GUID with delayed expansion.
setlocal enabledelayedexpansion
:::: Generate Random Hexidecimal GUID
:: Loop Once for each GUID Value = 32
:: 1. Check if Hyphen Insert Needed
:: 2. Generate Random Number = 0-15
:: 3. Convert Number to Hexidecimal
:: 4. Append to GUID
set "xGUID="
for /L %%n in (1,1,32) do (
if "%%~n"=="9" set "xGUID=!xGUID!-"
if "%%~n"=="13" set "xGUID=!xGUID!-"
if "%%~n"=="17" set "xGUID=!xGUID!-"
if "%%~n"=="21" set "xGUID=!xGUID!-"
set /a "xValue=!random! %% 16"
if "!xValue!"=="10" set "xValue=A"
if "!xValue!"=="11" set "xValue=B"
if "!xValue!"=="12" set "xValue=C"
if "!xValue!"=="13" set "xValue=D"
if "!xValue!"=="14" set "xValue=E"
if "!xValue!"=="15" set "xValue=F"
set "xGUID=!xGUID!!xValue!"
)
endlocal & if not "%~1"=="" set "%~1=%xGUID%"
goto :eof
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GUID2 <xReturn>
:: Generate a GUID without delayed expansion.
setlocal
set "xGUID="
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
set "xGUID=%xGUID%-"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
set "xGUID=%xGUID%-"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
set "xGUID=%xGUID%-"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
set "xGUID=%xGUID%-"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
call :AppendHex xGUID "%xGUID%"
endlocal & if not "%~1"=="" set "%~1=%xGUID%"
goto :eof
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GUID3 <xReturn>
:: Generate a GUID with delayed expansion and AppendHex.
setlocal enabledelayedexpansion
:::: Generate Random Hexidecimal GUID
:: Loop Once for each GUID Value = 32
:: 1. Check if Hyphen Insert Needed
:: 2. Append a Hexidecimal
set "xGUID="
for /L %%n in (1,1,32) do (
if "%%~n"=="9" set "xGUID=!xGUID!-"
if "%%~n"=="13" set "xGUID=!xGUID!-"
if "%%~n"=="17" set "xGUID=!xGUID!-"
if "%%~n"=="21" set "xGUID=!xGUID!-"
call :AppendHex xGUID "!xGUID!"
)
endlocal & if not "%~1"=="" set "%~1=%xGUID%"
goto :eof
:: by David Ruhmann
@bharatbhamare
Copy link

FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment