Generate GUID in Windows with batch file

Batch FileCommand LineCmdGuid

Batch File Problem Overview


How can I generate a GUID in a batch file running using the commandline in Windows?

Batch File Solutions


Solution 1 - Batch File

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

Solution 2 - Batch File

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%

Solution 3 - Batch File

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();

Solution 4 - Batch File

easy to do in powershell

[guid]::NewGuid()

Solution 5 - Batch File

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.

Solution 6 - Batch File

This will copy a new GUID to your clipboard:

POWERSHELL -c "[guid]::NewGuid().ToString().ToUpper()" | CLIP

Solution 7 - Batch File

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

Solution 8 - Batch File

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%

Solution 9 - Batch File

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.

Solution 10 - Batch File

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);
}

Solution 11 - Batch File

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%.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPratikView Question on Stackoverflow
Solution 1 - Batch FileTim RobinsonView Answer on Stackoverflow
Solution 2 - Batch Fileuser2441603View Answer on Stackoverflow
Solution 3 - Batch FilejmjView Answer on Stackoverflow
Solution 4 - Batch FileJonasView Answer on Stackoverflow
Solution 5 - Batch FileDirk VollmarView Answer on Stackoverflow
Solution 6 - Batch FileMichel de RuiterView Answer on Stackoverflow
Solution 7 - Batch FileefdummyView Answer on Stackoverflow
Solution 8 - Batch FilenpocmakaView Answer on Stackoverflow
Solution 9 - Batch FileDrew NoakesView Answer on Stackoverflow
Solution 10 - Batch FileBiswapriyoView Answer on Stackoverflow
Solution 11 - Batch FilerojoView Answer on Stackoverflow