#PSTip How to properly validate GUID parameter

Most of you probably know how to create a Globally Unique Identifier (GUID) with the following code:

[System.Guid]::NewGuid()

But, let’s say, I have a function that has a GUID parameter. To validate the given GUID as an input, I’d use a regular expression with the ValidatePattern attribute.

Function Test-Foo {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidatePattern('(\{|\()?[A-Za-z0-9]{4}([A-Za-z0-9]{4}\-?){4}[A-Za-z0-9]{12}(\}|\()?')]
        [string[]]$GUID
    )
    Begin{}

    Process{
        $_
    }

    End{}
}

The regular expression works great but doesn’t fully cover all the existing GUID formats. The ValidateScript attribute combined with the Parse method of the System.Guid object will help improve the validation.

Let’s see this in action:

Function Test-Bar {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidateScript({
            try {
                [System.Guid]::Parse($_) | Out-Null
                $true
            } catch {
                $false
            }
        })]
        [string[]]$GUID
    )
    Begin{}

    Process{
        $_
    }

    End{}
}

I’ll first create an array of 5 random GUIDs with different formats. The surrounding parenthesis display the content while assigning the output to a variable.

($ar = "N","D","B","P","X" | ForEach {
	[System.Guid]::NewGuid().ToString($_)
})

Let’s pass this array of 5 valid GUIDs to the first function that uses the regular expression:

The error above is expected as the regular expression doesn’t cover the 5th format. There is a second issue with the regular expression–it would report an invalid GUID that mixes 2 or 3 different formats as valid.

The Test-Foo function validates the above GUID although it isn’t valid.

Now, let’s try the second function that uses the validation script and parse method:

No error, all GUIDs are valid. The validation script method fixes the second issue as well:

Share on: