Skip to content

Instantly share code, notes, and snippets.

@azurekid
Last active October 12, 2023 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azurekid/e616b0029eeb61671a257a298b528786 to your computer and use it in GitHub Desktop.
Save azurekid/e616b0029eeb61671a257a298b528786 to your computer and use it in GitHub Desktop.
PowerShell function to create a GUID from a string value
<#
.SYNOPSIS
Generates a GUID from a given string value using MD5 hashing.
.PARAMETER Value
The string value to generate a GUID from.
.EXAMPLE
Get-Guid -Value "example string"
Returns a GUID generated from the string "example string".
.EXAMPLE
Get-Guid('Rogier Dijkman')
Returns a GUID generated from the string "Rogier Dijkman".
Guid
----
3f5b5733-f9ae-26da-1d2e-63601f7b3c20
.NOTES
Author: Rogier Dijkman
Date: 2023-10-10
#>
function Get-Guid {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Value
)
$MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$UTF8 = New-Object -TypeName System.Text.UTF8Encoding
$bitString = ([System.BitConverter]::ToString($MD5.ComputeHash($UTF8.GetBytes($Value)))).replace("-","").ToLower()
$guid = [System.Guid]::Parse($bitString)
return $guid
}
@azurekid
Copy link
Author

Get-Guid

This function can be used to create a GUID from a string value.
In some cases this can be useful if you need to generate a value that is consistent based on the input value.

C:\> Get-Guid('Rogier Dijkman')

Guid
----
3f5b5733-f9ae-26da-1d2e-63601f7b3c20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment