How can I generate a GUID in a batch file running using the commandline in Windows?
- 
                    There is no built-in command available that does that. Either write your own, or get an existing one. – Dirk Vollmar Nov 30 '10 at 11:57
- 
                    3BTW, MS-DOS has been dead for at least 10 years. The program that runs batch files is the command prompt. – Tim Robinson Nov 30 '10 at 11:58
- 
                    1http://stackoverflow.com/questions/569858/command-line-guid-for-unix-and-windows – onder Nov 30 '10 at 12:00
- 
                    9@Tim Robinson : Thanks for your input, looking forward for help on creating GUIDs. – Pratik Nov 30 '10 at 12:06
11 Answers
The Windows SDK comes with a tool called uuidgen (if you have Visual Studio, you'll have the Windows SDK, and you need to run the Visual Studio Command Prompt to set proper paths).
C:\>uuidgen
This will output a new GUID, e.g.
cc23b318-156e-473f-aa6e-517bf091a0f0
 
    
    - 66,030
- 26
- 140
- 208
 
    
    - 53,480
- 10
- 121
- 138
- 
                    Maybe download and install the Windows SDK separately? Although it's a lot for one utility - it would be quicker to write your own than to wait for the download. – Tim Robinson Nov 30 '10 at 12:20
- 
                    incases where i can't download due to some reasons what can be another alternative than SDK – Pratik Nov 30 '10 at 12:26
- 
                    If you have no other alternative you'll probably need to write your own GUID generator – Tim Robinson Nov 30 '10 at 12:27
- 
                    You need to use the -c option on uuidgen for a windows installer project file. – sweetfa Apr 07 '14 at 21:41
- 
                    1Funny thing, I have three different versions of VS but uuidgen was not in the `PATH` for me. Also, it doesn't seem to be really random but time-based (Unix versions of this utility have a `-r` and `-t` flags to specify if you want it random-based or time-based. – Camilo Martin Aug 24 '14 at 18:23
- 
                    
- 
                    
- 
                    This should not be marked correct, as most users do not have the SDK. Calling powershell from cmd is the way to go, as various answers below describe. – DAG Aug 07 '23 at 11:48
Try this if you have powershell environment.
FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )
Then ready Guid value from %NEWGUID%
 
    
    - 10,864
- 22
- 84
- 115
 
    
    - 321
- 3
- 2
- 
                    3If writing a batch file (`.bat`), don't forget to double the `%` in the `FOR` statement (so the line should read `FOR /F %%a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%%a )`) – youen Jul 28 '16 at 12:16
- 
                    2@Soundararajan powershell and .NET is available in all Windows versions since Vista – phuclv May 27 '17 at 03:27
easy to do in powershell
[guid]::NewGuid()
 
    
    - 291
- 3
- 2
- 
                    7Even easier now: [`New-Guid`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-guid) – Herohtar Nov 07 '19 at 16:50
1.Create a file named myuuid.vbs with the content
set obj = CreateObject("Scriptlet.TypeLib")  
WScript.StdOut.WriteLine Mid(obj.GUID, 2, 36)
2.goto command prompt
cscript //NoLogo myuuid.vbs
Using JAVA code
    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();
 
    
    - 10,029
- 11
- 83
- 152
 
    
    - 237,923
- 42
- 401
- 438
- 
                    @Pratik this is VB Script code that will do your task you don't need any special env for this just follow 2 steps mentioned – jmj Nov 30 '10 at 12:10
- 
                    @Pratik have you followed step 2 exactly mentioned ? from command line it should be "script //NoLogo myuuid.vbs " – jmj Nov 30 '10 at 12:15
- 
                    
- 
                    1@Pratik http://www.devguru.com/technologies/vbscript/quickref/filesystemobject.html here is example how to create file using VBS and it is already stored in variable OBJ , I am not much aware about VBS otherwise I would have told you :) – jmj Nov 30 '10 at 12:28
- 
                    1Install cygwin, and ensure you have the _util-linux_ package. That will give you a `uuidgen` command. – bobbogo Jul 08 '14 at 14:51
There is no built-in command available that does that. Either write your own, or get an existing one.
A simple program that outputs a GUID to the console could be written using C#:
class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine(System.Guid.NewGuid().ToString());
    }
}
Place the above snippet in a file name guidgen.cs and then compile it using the following command line (.NET Framework 2.0 would have to be installed on your system):
%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe guidgen.cs 
This will create an executable named guidgen.exe.
 
    
    - 172,527
- 53
- 255
- 316
- 
                    seems like C#. well i have an very old pc with no .NET framwork will this work or do you some solution other than this ? – Pratik Nov 30 '10 at 12:10
- 
                    @Pratik: How old? Is it really running MS-DOS? Do you have any compiler available at all? – Dirk Vollmar Nov 30 '10 at 12:17
- 
                    dos version 5.1.2600 on Windows XP. i don't have any idea about compilers – Pratik Nov 30 '10 at 12:19
- 
                    Any idea why I'm not able to `clip` the output in bash as `$ guidgen | clip`? I'm having to wrap this in a function and export it as `guidgen() { echo "$(guidgen.exe)"; } export -f guidgen` (see https://stackoverflow.com/questions/1500499/how-do-you-call-a-function-defined-in-bashrc-from-the-shell/1500592#1500592). – hIpPy Nov 13 '18 at 21:19
- 
                    The ToString call is not necessary as WriteLine has an overload for System.Object, which does the same. Not sure if creating the string instance is worse than boxing performance-wise, but it certainly is longer and less readable. – Palec Nov 24 '19 at 20:25
This will copy a new GUID to your clipboard:
POWERSHELL -c "[guid]::NewGuid().ToString().ToUpper()" | CLIP
Recent versions have cmdlets like this:
POWERSHELL -c "New-Guid | Set-Clipboard"
 
    
    - 7,131
- 5
- 49
- 74
If you want to do it with pure cmd commands, you can use something like that (this is not a true GUID but it can help depending on your context) :
@call :GetGuid NewGuid
@echo My new GUID : %NewGuid%
@goto :eof
:GetGuid
 @set _guid=%computername%%date%%time%
 @set _guid=%_guid:/=%
 @set _guid=%_guid:.=%
 @set _guid=%_guid: =%
 @set _guid=%_guid:,=%
 @set _guid=%_guid::=%
 @set _guid=%_guid:-=%
 @set %1=%_guid%
@goto :eof
 
    
    - 684
- 1
- 8
- 9
- 
                    Save the contents as a .BAT file and run it. Works on my machine (Windows 8). – Aaron D Jan 04 '14 at 00:00
- 
                    Better to do it in a .CMD file to be interpreted by CMD.EXE instead of COMMAND.COM ;-) http://www.differencebetween.net/technology/difference-between-cmd-and-bat/ – efdummy Sep 18 '14 at 21:58
- 
                    why don't just put `@echo off` at the beginning instead of prefixing every commands with `@`? – phuclv May 27 '17 at 03:24
- 
                    Prefixing every commands with a @ is useful when you are debugging your script. If you suspect that a line is not running well, just suppress the @ for this line and re-run your script. You will see at the console what's happening. – efdummy Jul 15 '17 at 00:01
try with uuid.bat
Without arguments it will just echo the generated uuid:
c:\>uuid.bat
2186cb38-d649-4c99-b7cc-c505e4dae9c2
If argument is passed it will be stored in a variable with the same name:
call uuid.bat my_uuid
echo %my_uuid%
94772f66-8bca-468e-9e9a-de0d9ee05cc1
For windows formatted GUIDs you can use guid.bat:
for /f %a in ('guid.bat') do set guid=%a
echo %guid%
 
    
    - 55,367
- 18
- 148
- 187
If you have dotnet installed:
$ dotnet tool install -g guid
$ guid
bf4be9d0-7d1b-485c-a435-d07fd7b892f0
$ guid help
Usage:
    guid [option]
Where [option] is an optional single character that controls formatting:
    N    B382C3F44E604D08B48A2D342A659B4E
    D    B382C3F4-4E60-4D08-B48A-2D342A659B4E
    B    {B382C3F4-4E60-4D08-B48A-2D342A659B4E}
    P    (B382C3F4-4E60-4D08-B48A-2D342A659B4E)
When unspecified, 'd' formatting is used. Use lowercase [option] for lowercase output.
 
    
    - 300,895
- 165
- 679
- 742
One could abuse bitsadmin to generate a GUID.  This will be profoundly faster than calling PowerShell from a Batch script.
@echo off & setlocal
for /f "delims={}" %%I in ('bitsadmin /rawreturn /create guid') do set "GUID=%%~I"
>NUL bitsadmin /cancel {%GUID%}
echo Your new GUID is %GUID%.  Neat.
If you want to keep the surrounding braces, remove "delims={}" and replace {%GUID%} with %GUID%.
 
    
    - 24,000
- 5
- 55
- 101
If the system OS does not have Windows SDK but does have a C compiler with mingw-w64 toolchain then compile this small program to generate random GUID. Imported functions are UuidCreate (rpcrt4.lib) to create random UUID and StringFromCLSID (ole32.lib) to convert UUID to wide string.
#include <Windows.h>
#include <stdio.h>
/*
 * int UuidCreate(GUID *id);
 * int StringFromCLSID(GUID *id, wchar_t **str);
 * Libraries: Rpcrt4.lib Ole32.lib
 */
int main(void)
{
    GUID id;
    wchar_t *str = NULL;
    UuidCreate(&id);
    StringFromCLSID(&id, &str);
    wprintf(L"%ls\n", str);
}
 
    
    - 3,491
- 3
- 22
- 42
 
    