Reversing GUIDs

by Jun 27, 2013

Active Directory and other services sometimes use a custom GUID format. To convert a GUID to that format, all blocks of the GUID need to be reversed. In addition, the second and third block need to be exchanged.

PowerShell can easily do that conversion:

$sampleGUID = [System.GUID]::NewGuid().Guid

$code = 
{
    $text = $_.ToCharArray()
    [Array]::Reverse($text)
    -join $text
}

$blocks = $sampleGUID.Split('-') | ForEach-Object -Process $code
$blocks[1], $blocks[2] = $blocks[2], $blocks[1]
$newGUID = $blocks -join '-'

$sampleGUID
$newGUID

First, the code creates a sample GUID to play with. It then separates the blocks of the GUID by splitting it at "-". Each block is then reversed.

Finally, blocks 1 and 2 (index starts at 0, so these are blocks 2 and 3 really) are exchanged. The result will look similar to this:

Twitter This Tip! ReTweet this Tip!