Creating GUIDs in PowerShell


When you work with SharePoint, you end up working a lot with both GUIDs and with PowerShell. Strangely enough, the two together don’t seem to be needed very much but eventually their paths cross. GUIDs in PowerShell are amazingly simple to create but the web is chock full of misinformation and insanely complicated suggestions. I’ve even seen some folks recommend passing parameters to New-Object!

So, just for the sake of clarity, here is how to create GUIDs in PowerShell.  It really doesn’t get much simpler than this!

# Create an empty GUID

   $Id = [GUID]::Empty


# Create a new GUID

   $Id = [GUID]::NewGuid()


# Create a GUID with a value

   $Id = [GUID]("b2e92f11-7f65-41d1-acec-ba051b418bdf")

There’s nothing to it but some people choose to make this so complicated.

Update – 5/31/13 – I discovered a way to make it even simpler!

Advertisements

Posted on February 22, 2012, in Development, Scripting and tagged , , . Bookmark the permalink. 2 Comments.

  1. …and to generate a random GUID as a string in uppercase use this syntax:

    $Str=[System.GUID]::NewGuid().ToString().ToUpper()

    This will generate a unique GUID everytime it is called.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s