Skip to content

Instantly share code, notes, and snippets.

@f-bader
Forked from azurekid/Get-Guid.ps1
Created October 12, 2023 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f-bader/8951911158e89ad3486030a479ce3c14 to your computer and use it in GitHub Desktop.
Save f-bader/8951911158e89ad3486030a479ce3c14 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment