Home » PowerShell » PowerShell Convert String to GUID

PowerShell Convert String to GUID

To convert string to guid in PowerShell, it uses the Parse() method of the [System.Guid] class that takes string guid as input parameter and returns a valid GUID which consists of 32 hexadecimal digits with 4 dashes.

Conversion of string to GUId can be useful when it needs to be stored in the database, comparing two GUIDs, and generating test data for an application where a unique identifier GUID is required.

In this article, we will discuss how to use the Parse method of Guid class in PowerShell to convert string to guid.

Convert String to GUID in PowerShell

[System.Guid] class provides a Parse method that takes the string representation of GUID and converts the string to guid in PowerShell.

# Assign 32 hexadecimal digits to string
$str = "6F9619FF-8B86-D011-B42D-00C04FC964FF"

# Use Parse method of Guid class for conversion 
$guid = [System.Guid]::Parse($str)  
Write-Host $str

In the above PowerShell script, the $str variable contains the string representation of the GUID ( 32 hexadecimal digits with 4 dashes). To convert string to guid in PowerShell, it uses the Parse() method of the Guid class and returns the Guid as a unique identifier.

The output of the above PowerShell script after converting the string to GUID is:

PowerShell Convert String to Guid
PowerShell Convert String to Guid

The input string should have a valid GUID format, which is 32 hexadecimal digits with dashes. If the input string is not in a valid GUID format, it will throw an error.

Cool Tip: How to convert guid to string in PowerShell!

Conclusion

I hope the above article on how to convert string to GUID in PowerShell using the Parse() method is helpful to you.

GUID class provides methods like NewGUID() to generate a GUID and other properties like GUID that return the string representation of a GUID.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.