Thursday 4 April 2013

Using Powershell to generate new Guids

Guids, or Globally Unique Identifiers, are often used by developers and others to attribute a unique id to an arbitrary object. For example, those who have used WiX to create MSI-based installers, often need to create a new GUID all the time while writing the WiX code for the installer. Wouldn't it be great to use a Powershell command line to just generate new GUIDs? Here is a sample script how to do this:

            
<#
    .SYNOPSIS
        Creates a new Guid object and displays its GUI to the screen
    .DESCRIPTION
        Uses static System.Guid.NewGuid() method to create a new Guid object
    .EXAMPLE
        1. Create a new Guid is easy:
        New-Guid
#>            
function New-Guid () {            
            
 [System.Guid] $guidObject = [System.Guid]::NewGuid()            
 [Windows.Forms.Clipboard]::Clear();            
 [Windows.Forms.Clipboard]::SetText($guidObject.Guid)            
 Write-Host $guidObject.Guid            
            
}            
            
New-Guid


To call the function New-Guid just type its name. You will get new GUIDs without a hassle:


PS C:\users\Tore Aurstad> New-Guid
0d2cc38f-e6de-4049-ae6a-f168bf1ea670

PS C:\users\Tore Aurstad> New-Guid
f99044b3-a8a6-4092-a520-c58ae259a19e

PS C:\users\Tore Aurstad> New-Guid
4de11003-8bbf-4415-8fe3-e7ecf4be9ee0

PS C:\users\Tore Aurstad> New-Guid
cac473e7-1f01-4d2f-b99c-4fdb25ab9da1

PS C:\users\Tore Aurstad> 



Of course, this is very similar to creating a Console application with C#, we just call the static method NewGuid on the Guid class. In Powershell, variables can be strongly typed as shown here and to call a static method you just write [MyNamespace.MyClass]::MyStaticMethod. Note the double colon marks in the middle.

In addition to generating a new Guid, the clipboard contents is set to the Guid value, which means you can paste in the text where you want. I did not have to import any additional modules to reach the static function [Windows.Forms.Clipboard]::Clear() and [Windows.Forms.Clipboard]::SetText().
Share this article on LinkedIn.

4 comments:

  1. I'm at work now. I found an even more powerful way now, you can paste the following in your $profile file:

    notepad $profile

    Add:

    if ($(get-alias Out-Clipboard) -eq $null){
    new-alias Out-Clipboard $env:SystemRoot\system32\clip.exe
    }

    function New-Guid($numberOfGuids = 1, $upperCase = $true){
    <#
    .SYNOPSIS
    Creates a new Guid object and displays its GUI to the screen
    .DESCRIPTION
    Uses static System.Guid.NewGuid() method to create a new Guid object
    .EXAMPLE
    1. Create a new Guid is easy:
    New-Guid
    #>

    [string] $result = "";
    foreach ($i in 0..$($numberOfGuids-1)){

    [System.Guid] $guidObject = [System.Guid]::NewGuid()

    $guid = $guidObject.Guid.ToString()
    if ($upperCase){
    $guid = $guid.ToUpper()
    }
    $result += $guid + "`n"
    }

    $result | Out-Clipboard

    return $result

    }

    ReplyDelete
  2. The code sample above can be used such as:

    C:\toaurs-he\opplan4hg\source [Doculive_Integration_250313 +5 ~6 ^1]> new-guid 4 $true
    61DD7EAB-DC8E-46E8-BCC9-7C754A9C50D0
    D77C92C1-2F47-4C63-A8A1-E790C7B29FA0
    9879189F-6426-4652-86B3-D2F48FC63456
    68200098-C9DD-4AF8-8D43-2B88F3071F46

    C:\toaurs-he\opplan4hg\source [Doculive_Integration_250313 +5 ~6 ^1]> new-guid 3 $false
    a7b803e7-4277-46ed-83eb-3b3c1e9f408b
    5854264c-1e1e-4e71-a6a6-b6378d36d099
    9a5292db-d105-4b36-a1ba-e9ed0f2cb32a

    C:\toaurs-he\opplan4hg\source [Doculive_Integration_250313 +5 ~6 ^1]>

    ReplyDelete
  3. Yes, you can omit "System" here. You are correct, [guid]::NewGuid() is quicker to type than [System.Guid]::NewGuid().

    ReplyDelete