Creating new GUIDs in various formats

by Apr 15, 2011

You can easily create new GUIDs (Globally Unique Identifiers) using the GUID type and its NewGUID() method. But did you know that you can use a GUIDs toString() method to create GUIDs in a number of different formats? Have a look:

$guid = [GUID]::NewGUID()
"N","D","B","P" | ForEach-Object {'[GUID]::NewGuid().ToString(''{0}'') = {1}' -f $_, $GUID.ToString($_)}
This will create a list of different ways to  create a GUID and shows the result:
[GUID]::NewGuid().ToString('N') = 94a5feaffa0848668f055fea268be867
[GUID]::NewGuid().ToString('D') = 94a5feaf-fa08-4866-8f05-5fea268be867
[GUID]::NewGuid().ToString('B') = {94a5feaf-fa08-4866-8f05-5fea268be867}
[GUID]::NewGuid().ToString('P') = (94a5feaf-fa08-4866-8f05-5fea268be867)

 

Twitter This Tip!
ReTweet this Tip!