Last active
November 5, 2024 11:33
-
-
Save azurekid/e616b0029eeb61671a257a298b528786 to your computer and use it in GitHub Desktop.
PowerShell function to create a GUID from a string value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.