21

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

17

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

powershell -Command "[guid]::NewGuid().ToString()"
| improve this answer | |
  • You can make it a reusable command within PowerShell with. function uuid { [guid]::NewGuid().ToString() }. Just run "uuid" as needed. – Alain O'Dea Jun 5 '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 at 21:39
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,"{",""),"}","")
| improve this answer | |
  • 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

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

| improve this answer | |
  • seems like it's only good for 1 guid – drzaus Jan 15 '14 at 21:24
  • 4
    This does NOT generate a GUID. It just shows an existing guid. DO NOT USE this if you want a unique GUID – Air2 Mar 27 '14 at 13:55
  • 3
    Relevant XKCD: xkcd.com/221 – TripShock May 11 '15 at 15:33
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 ...

| improve this answer | |
1

From MSDN Library: Generating Interface UUIDs.

| improve this answer | |
  • Do you know where I can download this "uuidgen" from? – appshare.co Jun 23 '10 at 11:24
  • It comes with Visual Studio - I'd expect it to come with the express edition of Visual C++ which I believe is a free download, as well – Rowland Shaw Jun 23 '10 at 11:34
  • 1
    msdn.microsoft.com/en-us/library/aa373930%28VS.85%29.aspx says that the uuidgen utility (Uuidgen.exe) is automatically installed when you install the Platform Software Development Kit (SDK). – Mehper C. Palavuzlar Jun 23 '10 at 11:35
  • Is it not available as a seperate download, as I have no use for the SDK or for Visual Studio. – appshare.co Jun 23 '10 at 11:36
  • 1
    @Zubair: Install SDK, get uuidgen, uninstall SDK. Anything else is a copyright violation. – harrymc Jun 23 '10 at 11:48
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);
}
| improve this answer | |
1

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"
| improve this answer | |
0
  FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.