How to generate a random GUID in Visual Studio 2022

Jamie Burns
2 min readApr 29, 2022

GUIDs are great.

e849c06e-5cd6-4eae-9749-1c76fd8ebfcf

There’s something a little mystical about a 32-character string that’s guaranteed* to be unique, no matter how many times you generate one.

I’ve found that when I’m writing unit tests, I have to generate a lot of GUIDs. In tests where these GUIDs are used as references, I like to hard-code them in the test (instead of using Guid.NewGuid() to create a new one each time), so that if I have a failed assertion that is comparing them, I can easily see where the GUID has come from.

Visual Studio 2022 already has a way of inserting a GUID, but it’s a little clunky. Head up to the ‘Tools’ toolbar, and go to ‘Create GUID’:

The ‘Create GUID’ menu item in Visual Studio 2022

This opens up a new window, with options to say what kind of GUID you want, and the ability to copy a new one:

‘Create GUID’ window in Visual Studio 2022

I find that that the format of GUID that I usually want isn’t one of these options. Usually, I’m trying to hard-code a string that can be parsed, so all I want to generate is the contents of the string:

So the solution provided by Visual Studio is not exactly ideal — it doesn’t generate the GUID in the format I need, plus I need to go to this extra window to actually do it.

Fortunately, there’s a better option, and yet again, it’s provided by brilliant Mads Kristensen: Insert Guid.

This is another free extension for Visual Studio 2022 (available in the marketplace), which does exactly what you’d expect it to do. Just search for ‘Insert Guid’, or install from the marketplace directly.

Searching for the ‘Insert Guid’ extension in Visual Studio 2022

Once installed, you can insert a newly-generated GUID either from the toolbar, or (even better) with the keyboard shortcut of Ctrl+K, Ctrl+Space.

It just works.

Whenever I need a new GUID, it’s just a quick Ctrl+K, Ctrl+Space and the new GUID is right there. There’s nothing else to it.

So if you find your need new GUIDs yourself, give this extension a go.

*Well, not exactly guaranteed to be unique, but pretty close

--

--

Jamie Burns

Software Engineer, founder of Bungalow64 Technologies