Last active
May 31, 2025 13:15
-
-
Save MyITGuy/d3e039c5ec7865edefc157fcd625a20a to your computer and use it in GitHub Desktop.
PowerShell: Convert GUID to/from Packed
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
function ConvertFrom-PackedGuid { | |
<# | |
.SYNOPSIS | |
Converts a packed globally unique identifier (GUID) string into a GUID string. | |
.DESCRIPTION | |
Takes a packed GUID string and breaks it into 6 parts. It then loops through the first five parts and reversing the order. It loops through the sixth part and reversing the order of every 2 characters. It then joins the parts back together and returns a GUID. | |
.EXAMPLE | |
ConvertFrom-PackedGuid -PackedGuid '2820F6C7DCD308A459CABB92E828C144' | |
The output of this example would be: {7C6F0282-3DCD-4A80-95AC-BB298E821C44} | |
.PARAMETER PackedGuid | |
A packed globally unique identifier (GUID) string. | |
#> | |
[CmdletBinding()] | |
[OutputType([System.String])] | |
param ( | |
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Mandatory)] | |
[ValidatePattern('^[0-9a-fA-F]{32}$')] | |
[ValidateScript( { [System.Guid]::Parse($_) -is [System.Guid] })] | |
[System.String]$PackedGuid | |
) | |
process { | |
Write-Verbose "PackedGuid: $($PackedGuid)" | |
$GuidString = ([System.Guid]$PackedGuid).ToString('N') | |
Write-Verbose "GuidString: $($GuidString)" | |
$Indexes = [ordered]@{ | |
0 = 8 | |
8 = 4 | |
12 = 4 | |
16 = 2 | |
18 = 2 | |
20 = 12 | |
} | |
$Guid = '' | |
foreach ($index in $Indexes.GetEnumerator()) { | |
$Substring = $GuidString.Substring($index.Key, $index.Value) | |
Write-Verbose "Substring: $($Substring)" | |
switch ($index.Key) { | |
20 { | |
$parts = $Substring -split '(.{2})' | Where-Object { $_ } | |
foreach ($part In $parts) { | |
$part = $part -split '(.{1})' | |
Write-Verbose "Part: $($part)" | |
[System.Array]::Reverse($part) | |
Write-Verbose "Reversed: $($part)" | |
$Guid += $part -join '' | |
} | |
} | |
default { | |
$part = $Substring.ToCharArray() | |
Write-Verbose "Part: $($part)" | |
[System.Array]::Reverse($part) | |
Write-Verbose "Reversed: $($part)" | |
$Guid += $part -join '' | |
} | |
} | |
} | |
[System.Guid]::Parse($Guid).ToString('B').ToUpper() | |
} | |
} | |
function ConvertTo-PackedGuid { | |
<# | |
.SYNOPSIS | |
Converts a GUID string into a packed globally unique identifier (GUID) string. | |
.DESCRIPTION | |
Takes a GUID string and breaks it into 6 parts. It then loops through the first five parts and reversing the order. It loops through the sixth part and reversing the order of every 2 characters. It then joins the parts back together and returns a packed GUID string. | |
.EXAMPLE | |
ConvertTo-PackedGuid -Guid '{7C6F0282-3DCD-4A80-95AC-BB298E821C44}' | |
The output of this example would be: 2820F6C7DCD308A459CABB92E828C144 | |
.PARAMETER Guid | |
A globally unique identifier (GUID). | |
#> | |
[CmdletBinding()] | |
[OutputType([System.String])] | |
param ( | |
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Mandatory)] | |
[ValidateScript( { [System.Guid]::Parse($_) -is [System.Guid] })] | |
[System.Guid]$Guid | |
) | |
process { | |
Write-Verbose "Guid: $($Guid)" | |
$GuidString = $Guid.ToString('N') | |
Write-Verbose "GuidString: $($GuidString)" | |
$Indexes = [ordered]@{ | |
0 = 8 | |
8 = 4 | |
12 = 4 | |
16 = 2 | |
18 = 2 | |
20 = 12 | |
} | |
$PackedGuid = '' | |
foreach ($index in $Indexes.GetEnumerator()) { | |
$Substring = $GuidString.Substring($index.Key, $index.Value) | |
Write-Verbose "Substring: $($Substring)" | |
switch ($index.Key) { | |
20 { | |
$parts = $Substring -split '(.{2})' | Where-Object { $_ } | |
foreach ($part In $parts) { | |
$part = $part -split '(.{1})' | |
Write-Verbose "Part: $($part)" | |
[System.Array]::Reverse($part) | |
Write-Verbose "Reversed: $($part)" | |
$PackedGuid += $part -join '' | |
} | |
} | |
default { | |
$part = $Substring.ToCharArray() | |
Write-Verbose "Part: $($part)" | |
[System.Array]::Reverse($part) | |
Write-Verbose "Reversed: $($part)" | |
$PackedGuid += $part -join '' | |
} | |
} | |
} | |
[System.Guid]::Parse($PackedGuid).ToString('N').ToUpper() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment