Thursday, October 11, 2012

How to generate a GUID at command line in Windows?

Well, like me who works with WIX and have to deal with lots of GUIDs, you might come across a situation where you may need to generate a GUID at command prompt during a procedure. I faced this while working with WIX based patches and unfortunately, like in other WIX templates i.e. Product, Module, PatchCreation doesn’t support Id=”*” where at the place of *, WIX compiler generates a new GUID each time.

So had to search for a command line utility in order to generate GUID randomly. Actually you can program such things even in VBScript and JScript, but I was looking for some built-in thing, to avoid adding new application to our build machine.

And I found UUIDGEN.exe. Yes, I know, Ancient C programmers are very much aware of this, but I only heard about it onceSmile with tongue out

It comes with almost every Visual Studio and with Windows SDK Tool. On my machine, it was present at following locations:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\Uuidgen.Exe

C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe

And here is its command line help:

"C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe" /?
Microsoft UUID Generator v1
.01 Copyright (c) Microsoft Corporation. All rights reserved
usage: uuidgen [-xisconvh]
x - Generate sequential
(V1) UUIDs
i - Output UUID in an IDL interface template
s - Output UUID as an initialized C struct
c - Output UUID in upper case
o
<filename> - redirect output to a file, specified immediately after o
n
<number> - Number of UUIDs to generate, specified immediately after n
v - display version information about uuidgen
h
,? - Display command option summary


In older versions of UUIDGEN.exe you might not getting option "-c" i.e. output upper case. So to generate a quick GUID, simply issue:
”C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe”


and you’ll get something like this:
5fae2556-299f-44dc-af2a-9afa3948e8a2 


and if you need Upper case issue same command by adding –c i.e.


“C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe” -c


and you should one in upper case:
2C4E4CD9-4635-42B6-A88A-099E952B95E8 .


 


How to use UUIDGEN in bat/cmd scripts?


Microsoft has provided “For” command which can be used to run any other command, read files  etc etc and then further process their outputs. Here is how you can use For with UUIDGEN to save generated GUID into an environment variable:

for /f %i in ('UUIDGEN') do set aGUID=%i


and this would define an environment variable aGUID containing your generated GUID.


 


 

1 comment:

jim_peak said...

Nice! Thanks for taking the time to doc this... I was just wondering about the same question.