Use PowerShell to generate a GUID
June 2, 2022
Another quick automation using PowerShell, this time to solve the problem of quickly adding a new GUID to the clipboard.
The New-Guid
cmdlet will quickly create a random globally unique identifier,
with the below output.
Guid
----
8c312165-113b-4c30-91e9-e4e6edebcf0b
This is great, but I’ve got a couple of extra needs; I don’t need the -
characters, and I want the GUID to be added straight to my clipboard.
Ignoring the header is simple:
(New-Guid).Guid
That’ll result in just the below output.
8c312165-113b-4c30-91e9-e4e6edebcf0b
Removing the -
characters is simple as well.
(New-Guid).Guid.Replace('-', '')
Which results in the below.
8c312165113b4c3091e9e4e6edebcf0b
Finally, to add it to our clipboard, we can pipe our output to the
Set-Clipboard
cmdlet.
(New-Guid).Guid.Replace('-', '') | Set-Clipboard