Created
March 17, 2023 13:23
-
-
Save drlsdee/d4a60d6a3d4c01e12b6ed7b6776b332b to your computer and use it in GitHub Desktop.
This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid. See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring
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
# This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid | |
# See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring | |
enum PSGuidFormatProvider { | |
None = 0 # Only alphanumeric characters: 00000000000000000000000000000000 | |
Braces = 1 # Figure brackets, "{}": {00000000-0000-0000-0000-000000000000} | |
Dashes = 2 # Hyphens, "-": 00000000-0000-0000-0000-000000000000 | |
Parentheses = 3 # Round brackets, "()": (00000000-0000-0000-0000-000000000000) | |
Xadecimal = 4 # Groups of heXadecimal values: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} | |
} | |
enum PSGuidCase { | |
Lower = 0 | |
Upper = 1 | |
} | |
function Format-Guid { | |
[CmdletBinding()] | |
param ( | |
[Parameter(ValueFromPipeline)] | |
[guid] | |
$Guid = [guid]::NewGuid(), | |
[Parameter()] | |
[PSGuidFormatProvider] | |
$Format = [PSGuidFormatProvider]::Dashes, | |
[Parameter()] | |
[PSGuidCase] | |
$Case = [PSGuidCase]::Lower | |
) | |
begin { | |
[string]$formatProvider = $Format.ToString().Substring(0,1) | |
} | |
process { | |
if ($Case -ne [PSGuidCase]::Upper) { | |
return $Guid.ToString($formatProvider) | |
} | |
return $Guid.ToString($formatProvider).ToUpperInvariant() | |
} | |
end {} | |
} | |
Format-Guid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment