4 ways to generate a GUID

GUIDs have a lot of common uses : primary keys in distributed databases and caches, temporary file and directory names, identifiers for types in the COM world…

You can generate them using a wealth of tools, depending on your situation.

1) With GuidGen

GuidGen is an interactive tool bundled with Visual Studio and the Windows SDK : on my computer I’ve one located in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools and another in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin.

You can run GuidGen directly or you can run it from Visual Studio :

Visual Studio Create GUID

Visual Studio 2010 Create GUID

Both will display the same small UI with commands for generating new GUIDs in different formats and copying them to the clipboard.

GuidGen sample

GuidGen in action

2) With Guidgen Console

If you want a non-interactive (ie without UI) tool to be used in your batch scripts for instance you can use GuidgenConsole.

Here is a sample :

C:\>C:\opt\GuidGen\GuidGen.exe
f78f217c-8efc-4974-abae-ffb3bce4d6c8

3) With online tools

If you need interactive generation from anywhere you can use an online generator like :

4) With the .Net library

The most powerful tool is of course the .Net library itself, where GUIDs are represented by the Guid value-type, which is often used by the aforementioned tools.
If you need to generate GUIDs from .Net code this is the way to go.

Here is an implementation of an awesome GUIDs generator :

using System;

class MyGuidGen
{
    private static void Main()
    {
        Console.Write(Guid.NewGuid());
    }
}

And how to compile and use it :

>csc MyGuidGen.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

>MyGuidGen.exe
9d2a9104-add4-4af0-b72c-4362cd59db44
>MyGuidGen.exe
2b077434-be28-4941-9d22-3f7725c19795
>MyGuidGen.exe
2beb6579-ac8b-4581-82e2-acad598f9ce1

You know other ways ? Please share by leaving a comment.

12 thoughts on “4 ways to generate a GUID

  1. Here’s a vbscript implementation. Save as > MyGuidGen.vbs and run from there.
    Apologies in advance if this snippet doesn’t post properly.

    ' MyGuidGen.vbs
    '
    ' Based on:
    ' http://stackoverflow.com/questions/968756/how-to-generate-a-guid-in-vbscript
    Do
    	' Generate Type Library
    	Set TypeLib = CreateObject("Scriptlet.TypeLib")
    	' Remove enclosing brackets
    	NewGuid = Mid(TypeLib.Guid, 2, 36)
    	' Present an input box that can be copied from
    	result = InputBox("Press Cancel to Quit", "MyGuidGen", NewGuid)
    Loop Until result = ""
    
  2. Thank you for inviting us to share other ways to generate globally unique identifiers.

    You can try the free and tiny TapGuid app for an easy way to create, copy, send or share GUIDs on your Android smartphone or tablet:

    http://www.tecdrop.com/tapguid/
    https://play.google.com/store/apps/details?id=com.tecdrop.tapguid

    (As a UI extra, for each GUID, the app fills the screen with a different color shade, that is computed from the value of that ID.)

    • Interesting but what kind of use-cases does it cover ?
      Because more often than not GUID generation is included in an automated process.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove me you\'re human :) *