Friday, April 16, 2010

Generate New GUID by PowerShell

It's simple and easy to get GUID using PowerShell.
Just type the following line in PowerShell command prompt:
[System.Guid]::NewGuid().toString()
If you need GUID wrapped in curly braces modify the script line to:
"{"+[System.Guid]::NewGuid().toString()+"}"

For me the typical action after getting a GUID is to copy it to the clipboard so the following snippet is my favorite.
$guid="{"+[System.Guid]::NewGuid().toString()+"}"
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$dataObject = New-Object windows.forms.dataobject
$dataObject.SetData([Windows.Forms.DataFormats]::UnicodeText, $true, $guid)
[Windows.Forms.Clipboard]::SetDataObject($dataObject, $true)
write-host The following GUID has been copied to the clipboard: $guid
So once you run the last script you have a new GUID in the clipboard. As for me - that what I call convenient

No comments: