How to Generate a GUID or UUID in Batch Script
A GUID (Globally Unique Identifier), often called a UUID (Universally Unique Identifier), is a 128-bit number used to create an identifier that is for all practical purposes unique. These are essential in scripting and programming for generating unique temporary filenames, creating transaction IDs, or naming resources without the risk of collision. While batch scripting has no native CreateGUID() function, you can easily generate one by leveraging built-in Windows tools.
This guide will show you the two best methods for generating a GUID. We will cover the quick and simple legacy method using the uuidgen.exe tool (if available), and then the modern, recommended approach using a PowerShell one-liner that is guaranteed to work on all modern Windows systems.
What is a GUID?
A GUID is a randomly generated 32-character hexadecimal string, typically broken into five groups and enclosed in curly braces.
Example GUID: {123e4567-e89b-12d3-a456-426614174000}
The algorithm used to create them is designed so that the probability of two different computers ever generating the same GUID is infinitesimally small. This makes them perfect "unique IDs."
Method 1: Using uuidgen.exe (Legacy Tool)
The uuidgen.exe is a small utility that is sometimes included with the Windows SDK or Visual Studio. It may or may not be present on a standard Windows installation, which is its main drawback. However, if it exists on the system PATH, it's very simple to use.
C:\> uuidgen
The output is a clean, lowercase GUID without braces, which is easy to work with.
a2b1c3d4-e5f6-7890-1234-567890abcdef
Method 2 (Recommended): Using PowerShell
The most robust and universally compatible method for all modern Windows systems (Windows 7 and newer) is to call PowerShell. PowerShell has a built-in class for generating new GUIDs, and it's trivial to call this from a batch script.
This one-liner generates a new GUID object and outputs it as a string.
powershell -Command "[guid]::NewGuid()"
PowerShell's default output includes the hyphens but no curly braces.
123e4567-e89b-12d3-a456-426614174000
This method is guaranteed to work on any modern Windows machine, making it the superior choice for scripts that need to be portable.
Storing the GUID in a Variable
To use the GUID in your script, you need to capture the output of the command. A FOR /F loop is the standard way to do this.
An example of script that uses the recommended PowerShell method:
@ECHO OFF
SET "MyGUID="
ECHO Generating a new GUID...
REM The FOR /F loop executes the command and captures its output.
FOR /F %%G IN ('powershell -Command "[guid]::NewGuid()"') DO (
SET "MyGUID=%%G"
)
ECHO The generated GUID is: %MyGUID%
This is the standard pattern for capturing the output of any command into a variable.
Common Pitfalls and How to Solve Them
Problem: uuidgen.exe is Not Found
This is the most common issue with the legacy method. uuidgen.exe is not part of a standard Windows installation.
Example of output in case of error:
'uuidgen' is not recognized as an internal or external command,
operable program or batch file.
Solution: Use the PowerShell Method
The PowerShell method has no external dependencies and is the reliable fallback. For any script that needs to be shared or run on different machines, you should use the PowerShell method by default.
Problem: Removing Hyphens or Braces
Sometimes you need a GUID as a single, contiguous 32-character string without any hyphens or braces (e.g., 123e4567e89b12d3a456426614174000).
Solution: Modify the PowerShell Command or Use String Replacement
-
Modify the PowerShell Command (Recommended): You can tell PowerShell to format the GUID exactly as you need it.
REM 'N' format is 32 digits with no hyphens.
powershell -Command "([guid]::NewGuid()).ToString('N')" -
Use Batch String Replacement: If you've already captured a standard GUID, you can remove the hyphens afterwards.
SET "MyGUID_NoHyphens=%MyGUID:-=%"
ECHO GUID with no hyphens: %MyGUID_NoHyphens%
The PowerShell method is cleaner as it produces the desired output in a single step.
Practical Example: Creating a Uniquely Named Log File
This script uses the robust PowerShell method to generate a GUID and uses it to create a log file with a name that is guaranteed to be unique, which is perfect for systems where multiple processes might be creating logs simultaneously.
@ECHO OFF
SETLOCAL
SET "LOG_FOLDER=%TEMP%\MyAppLogs"
MKDIR "%LOG_FOLDER%" 2>NUL
ECHO --- Unique Log File Generator ---
ECHO.
REM --- Generate a unique ID ---
FOR /F %%G IN ('powershell -NoProfile -Command "([guid]::NewGuid()).ToString(''N'')"') DO (
SET "UniqueID=%%G"
)
REM --- Build the log filename ---
SET "LOG_FILE=%LOG_FOLDER%\Log_%UniqueID%.txt"
ECHO Creating unique log file:
ECHO "%LOG_FILE%"
(
ECHO Log created at %DATE% %TIME%
ECHO Unique ID: %UniqueID%
) > "%LOG_FILE%"
ECHO.
ECHO [SUCCESS] Log file created.
ENDLOCAL
Conclusion
While batch scripting doesn't have a native command to create a GUID, modern Windows provides built-in tools that make this task simple and reliable.
- The legacy
uuidgen.exetool is simple but not guaranteed to be present on a system. - The PowerShell
[guid]::NewGuid()method is the overwhelmingly superior and recommended choice. It is fast, flexible, works on all modern Windows systems, and can be customized to produce the exact format you need.
By using the PowerShell one-liner inside a FOR /F loop, you can easily generate and capture unique identifiers for any purpose in your batch scripts.