From time to time, I need to generate GUID to be used as Unique Id for whatever reason. When I have Visual Studio opened, I usually use the GUID tool that comes with it.
But sometimes I am developing in VS Code and opening Visual Studio just to get access to the GUID tool is a pain.
So why not add the functionality to PowerShell instead? You can have PowerShell available from inside VS Code Terminal. It's the perfect thing to add.
So how do we go about that?
You can, of course, create an alias to the guidgen.exe GUID generator tool from Visual Studio installation Common7\Tools folder.
Alternatively, you can add the following to your $PROFILE using ise $PROFILE
from inside PowerShell.
# Guid Generator
function guid()
{
$guid = [guid]::NewGuid().ToString().ToUpperInvariant();
return $guid
}
# Helper function to paste text from input pipe or parameter to Windows Clipboard
# Not necessary in PS v5, since it has built in Set-Clipboard, however we add a notification
# functionality in this one to see what has been copied to the clipboard.
# if you would rather use the built in Set-Clipboard, remove this function
function Set-Clipboard {
<#
.Synopsis
Sets the system clipboard with either output of a command/function/cmdlet or given text
.Description
This function takes the $input from the pipeline and sends it to the clipboard so that we can paste the output wherever we want.
.Parameter Text
Text string that you want to store in clipboard
.Example
Example 1:
Get-Process | Set-Clipboard
Example 1:
Set-Clipboard -Text "Copy this string to clipboard"
.Notes
NAME: Set-ClipBoard
AUTHOR: Sitaram Pamarthi
WEBSITE: http://techibee.com
#>
param (
$Text
)
$null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
if($text) {
$Clipboardtext = $text
[Windows.Forms.Clipboard]::SetText($Clipboardtext)
} else {
$temptext = $($input | out-string)
$Clipboardtext = $temptext.Substring(0, $temptext.Length - 2)
}
Write-Host "$Clipboardtext has been copied to the clipboard."
[Windows.Forms.Clipboard]::SetText($Clipboardtext)
}
# Helper function to generate guid and at the same time add it to the clipboard
function cguid()
{
guid | Set-Clipboard
}
Once you add and save it, just reload your $PROFILE and test it...
. $PROFILE
cguid
Now you should have new random GUID generated and ready to paste since it is automatically added to your Windows clipboard.