32

How can I generate a UUID from the command line in Windows XP? Something like "uuid" or "uuidgen" in Linux.

Mehper C. Palavuzlar
  • 55,164
  • 49
  • 193
  • 250
yazz.com
  • 3,311
  • 6
  • 32
  • 48
  • cross-site duplicate: [Generate GUID in Windows with batch file](https://stackoverflow.com/q/4313422/995714) – phuclv May 27 '17 at 03:57

10 Answers10

33

If powershell is installed this is a simple commandline to get a guid

powershell -Command "[guid]::NewGuid().ToString()"
Air2
  • 431
  • 4
  • 3
  • You can make it a reusable command within PowerShell with. function uuid { [guid]::NewGuid().ToString() }. Just run "uuid" as needed. – Alain O'Dea Jun 05 '13 at 17:33
  • I put this line in guid.bat in a PATH folder so it works in regular command prompt and PowerShell. – biscuit314 Jan 13 '20 at 21:39
  • 1
    for folks running this on `Git Bash`, stick this into a function in `.bashrc` as `function guid() { powershell -Command "[guid]::NewGuid().ToString()" }`. Invoke it by just typing `guid` and hitting `Enter`. – Dut A. Jan 29 '21 at 19:00
  • 1
    Now even `powershell -command new-guid`, if I am may copy answer from mentioned cross-site dupclicate. – bogec Feb 07 '21 at 17:45
7

Drop the following code into a new file name uuid.vbs

set obj = CreateObject("Scriptlet.TypeLib")
WScript.StdOut.WriteLine obj.GUID

Then you can run it from the command line like so:

cscript //NoLogo uuid.vbs

This will work on pretty much any computer that has the Windows Scripting Host installed - which certainly includes anything later than Windows 2000, and probably includes 95/98/ME as well... though I don't have an instance handy to check.

If you need to remove the braces, replace the last line with this

WScript.StdOut.WriteLine Replace(Replace(obj.GUID,"{",""),"}","")
Goyuix
  • 6,511
  • 4
  • 37
  • 48
  • 4
    Funny how in Windows it's only unique to each planet, but in Unix and related systems it's unique throughout the entire universe. – Bratch Jun 23 '10 at 19:45
3

Now in powershell, you can use built in New-GUID function:

For /f "tokens=* delims= " %%a in ('powershell -noP -c "& {(New-GUID).GUID}"') do set "GUID=%%~a"
Wasif
  • 7,984
  • 2
  • 19
  • 32
3

You can also use this command in a command prompt:
wmic path win32_computersystemproduct get uuid

bwDraco
  • 45,747
  • 43
  • 165
  • 205
test
  • 63
  • 1
  • 1
2

To copy a new GUID to the clipboard, use this command :

cmd /c powershell.exe -Command "[guid]::NewGuid().ToString()|Set-Clipboard"

You can run the command straight from the Start, Run dialog ( WinLogo + R ), then use Ctrl+V to paste the generated GUID, which WILL also save it into your Run dialog history - aka if you use it often it will pop-up as suggestion there ...

Yordan Georgiev
  • 153
  • 1
  • 8
2

From MSDN Library: Generating Interface UUIDs.

Mehper C. Palavuzlar
  • 55,164
  • 49
  • 193
  • 250
2

I found that uuidgen.exe is present inside my C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 directory, probably because of some components that I have installed along with Visual Studio.

So I simply added C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64 to my PATH and now it's easy to remember to type uuidgen like I'm used on macOS instead of having to remember a long command.

0xced
  • 570
  • 4
  • 9
1

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);
}
Biswapriyo
  • 10,831
  • 10
  • 47
  • 78
1

You can use the Windows Subsystem for Linux, eg install Ubuntu -

Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

$ uuidgen
(prints a uuid)
Brian Burns
  • 700
  • 8
  • 14
0
  FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )