Generating GUIDs

Today I noticed a developer leaving Visual Studio to go to guidgenerator.com to generate a single GUID. I tweeted this

and immediately thought to myself that there has to be a quicker way to get your hands on a shiny new GUID, without leaving the IDE, or at least locally on the machine. A surprising amount of people chimed in and as it turns out - there are quite a few ways to do this. Here are some.

Built-in Visual Studio tool

Visual Studio has a built-in tool to generate GUIDs:

Generate a GUID with Visual Studio

As you can see you have a bunch of formatting options and the ability to copy the result directly to your clipboard, in order to paste it directly into your code.

PowerShell/Package Manager Console

The newer versions of Visual Studio comes with the "Package Manager Console" built-in. It's basically a PowerShell prompt inside Visual Studio.

Since you can call .NET methods directly from PowerShell, it lets you do something like [Guid]::NewGuid() to generate a single GUID. If you want it on your clipboard, you can just pipe the result to clip.

If you need several GUIDs at the same time, you can easily generate a range as well:

Generate GUIDs with Package Manager Console

Of course this applies to regular command line PowerShell as well.

scriptcs

If you have scriptcs installed, it's just as easy to generate a range of GUIDs as PowerShell, but without the horrible syntax. Check it out.

Generate GUIDs with scriptcs

ProTip! Since both PowerShell and scriptcs are using the Guid.NewGuid method, which returns a Guid object, you can take advantage of its ToString format to tweak the way it's represented.

ReSharper

It turns out (I didn't know about this earlier) that R# has a "snippet" called nguid to insert a generated GUID directly into your code. It also lets you choose the format you want. Check it out.

Generate a GUID with ReSharper

F# Interactive

If you have the F# tooling installed (I'm not sure if it's installed by default, sadly), you have the "F# Interactive" window in Visual Studio, and probably also fsi.exe on your PATH. This'll also let you generate GUIDs in F# from both inside Visual Studio and on the command line.

Generate GUIDs with F# Interactive

Unfortunately this example is almost as verbose as PowerShell :( If you're an F# expert and have a better way of doing this, let me know.

UUIDGEN

If you pop open your Visual Studio Developer Command Prompt, you have a little tool called UUIDGEN.exe at your disposal. You can read more about it here. This will also let you generate multiple GUIDs in one go.

Generate GUIDs with UUIDGEN

A (simpler) version of this is also available for OS X, but I couldn't find a way for it to generate batches.

Others?

Do you have other cool ways to generate a GUID within Visual Studio or on the command line? I'd love to know about them :)

I'm sorry for all the GUIDs wasted in making this blog post. You know; once they are gone, they're gone for good.