-
-
Save f-bader/8951911158e89ad3486030a479ce3c14 to your computer and use it in GitHub Desktop.
PowerShell function to create a GUID from a string value
This file contains 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