Table of Contents [hide]
💡TL;DR
Use theToString()
method on System.Guid to convert a GUID to a string in PowerShell.
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.
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.
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.
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.
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.
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.
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.