Table of Contents
💡TL;DR
Use theToString()
method on System.Guid to convert a GUID to a string in PowerShell.
123456 $guid = [System.Guid]::NewGuid()$guidString = $guid.ToString()Write-Host $guid.GetType() - $guidWrite-Host $guidString.GetType() - $guidString
1234 System.Guid - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830System.String - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830
Before diving into the solutions, let’s understand what GUID is. In PowerShell, a Globally Unique Identifier (GUID) is a 128-bit identifier used to identify objects uniquely—for instance, folders, files, registry keys, and other system resources. Remember, GUIDs are not supposed to be unique in one folder, but they are unique across all networks and computers worldwide, making them beneficial in many scenarios.
The point is how a GUID is a 128-bit identifier, particularly how we can create it. First, we can generate a GUID using the New-Guid
cmdlet, which returns 32-bit hexadecimal digits in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
separated by hyphens; each hexadecimal digit represents 4-bits, so 32-bit hexadecimal value represents 128-bits.
To understand it better, take a randomly produced GUID as f81d4fae-7dec-11d0-a765-00a0c91e6bf6
, having five groups of hexadecimal digits separated by hyphens.
1 2 3 4 5 6 7 8 9 10 |
f81d4fae-7dec-11d0-a765-00a0c91e6bf6 f81d4fae -> 8 Hexadecimal Digits -> 32 bits 7dec -> 4 Hexadecimal Digits -> 16 bits 11d0 -> 4 Hexadecimal Digits -> 16 bits a765 -> 4 Hexadecimal Digits -> 16 bits 00a0c91e6bf6 -> 12 Hexadecimal Digits -> 48 bits So: f81d4fae-7dec-11d0-a765-00a0c91e6bf6 = 32-bit Hexadecimal Digits = 128 bits |
Every time we generate a GUID, it is so long and randomly produced, so the chances of two or more GUIDs being identical are very low, making it an extremely effective approach to identify objects in the distributed environment uniquely.
Using ToString()
Method
Use the ToString()
method to convert a GUID to a string in PowerShell.
1 2 3 4 5 6 |
$guid = [System.Guid]::NewGuid() $guidString = $guid.ToString() Write-Host $guid.GetType() - $guid Write-Host $guidString.GetType() - $guidString |
1 2 3 4 |
System.Guid - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830 System.String - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830 |
We used the NewGuid()
method of the Guid
class provided by the PowerShell .NET framework to create a new instance of the Guid structure. NewGuid()
method is often used to create temp folder on windows.
We stored this generated GUID in the $guid
variable with which we chained the ToString()
method to convert its type from Guid
to String
. This converted string was stored in the $guidString
variable. Next, we used the Write-Host
cmdlet to print the type and value of $guid
and $guidString
. However, the GetType()
method retrieved the object type.
Use ToString()
Method with Format Specifier
Use the ToString()
method with a format specifier to convert a GUID to a string in PowerShell.
1 2 3 4 5 6 |
$guid = [System.Guid]::NewGuid() $guidString = $guid.ToString("D") Write-Host $guid.GetType() - $guid Write-Host $guidString.GetType() - $guidString |
1 2 3 4 |
System.Guid - 24b9ca9d-5f9c-44d5-997b-7a411c8630e1 System.String - 24b9ca9d-5f9c-44d5-997b-7a411c8630e1 |
This example is the same as we learned in the previous section. Here, we passed the "D"
format specifier to the ToString()
method as an argument to format the GUID as a string in a particular format. In this example, the converted string was represented as a sequence of 32 hexadecimal digits, shown in five groups; a hyphen separates each group; see the above output.
Did you find the pattern of a converted string the same as we got for an example code in the previous section? Yes? You are correct because $guid.ToString("D")
and $guid.ToString()
produced the same results as "D"
is the default value. See the following example for a demonstration.
1 2 3 4 5 6 7 8 |
$guid = [System.Guid]::NewGuid() $guidString1 = $guid.ToString("D") $guidString2 = $guid.ToString() Write-Host $guid.GetType() - $guid Write-Host $guidString1.GetType() - $guidString1 Write-Host $guidString2.GetType() - $guidString2 |
1 2 3 4 5 |
System.Guid - f098496d-3972-4e3a-8513-d5737722e2be System.String - f098496d-3972-4e3a-8513-d5737722e2be System.String - f098496d-3972-4e3a-8513-d5737722e2be |
See, the strings produced by the ToString()
and ToString("D")
are in identical format. So let’s practice the code with the N
format specifier, which produces the exact string without hyphens.
1 2 3 4 5 6 7 8 |
$guid = [System.Guid]::NewGuid() $guidString1 = $guid.ToString("D") $guidString2 = $guid.ToString("N") Write-Host $guid.GetType() - $guid Write-Host $guidString1.GetType() - $guidString1 Write-Host $guidString2.GetType() - $guidString2 |
1 2 3 4 5 |
System.Guid - 7b799c22-dc9c-49e0-8131-5668ff8480c9 System.String - 7b799c22-dc9c-49e0-8131-5668ff8480c9 System.String - 7b799c22dc9c49e081315668ff8480c9 |
We have other format specifiers as well that you can find below:
Format Specifier | Description |
---|---|
N | 32 digits: 00000000000000000000000000000000 |
D | 32 digits, separated by -: 00000000-0000-0000-0000-000000000000 |
B | 32 digits, separated by -, enclosed in {}: {00000000-0000-0000-0000-000000000000} |
P | 32 digits, separated by -, enclosed in (): (00000000-0000-0000-0000-000000000000) |
X | 04 hexadecimal values wrapped within {}, where the 4th value is a subset of 8 hexadecimal values, enclosed in {} too: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} |
In the above table,
0
represents a digit. If the given format specifier isnull
or an empty string("")
, the"D"
will be used. All format specifiers can be used in uppercase or lowercase; both forms will have the same effect. Note that theToString()
method will generate FormatException if any invalid format specifier is given.
Using [string]
Type Accelerator
Use the [string]
type accelerator to convert the specified GUID to string in PowerShell.
1 2 3 4 5 6 |
$guid = [System.Guid]::NewGuid() $guidString = [string]$guid Write-Host $guid.GetType() - $guid Write-Host $guidString.GetType() - $guidString |
1 2 3 4 |
System.Guid - 9bc7a33a-b415-428f-92b2-1245b6cb03ed System.String - 9bc7a33a-b415-428f-92b2-1245b6cb03ed |
In this code snippet, we did not use the ToString()
method but the [string]
type accelerator, which converted the type of $guid
from Guid
to String
.
That’s all about powerShell convert Guid to String.