Things I Learned #20: Creating GUIDs at the command line in Windows and Mac

3 minute read til learning   macos   powershell   dotnet   terminal Comments

This post was updated on July 25, 2023 @ 4pm eastern time

It’s been a few months since I’ve written one of these, but this one has been on my Trello board for quite some time, so I figured I should tackle it and move the card to Done. I’m also definitely NOT procrastinating on the conference talks I have coming up in the next few weeks. Nope…not at all.

Much of my day-to-day client work is done inside an Azure-hosted Windows virtual machine where I run Visual Studio and some client-specific applications. Some of my work is done in the Azure Portal, and for that work, I tend to it from a browser running on my Mac. A lot of this work includes interacting with Service Bus queues. For development purposes, each member of the team has their own Service Bus set up so we can do our initial development and testing in an isolated environment.

For example, I’ve been working on several Azure Functions that consume messages from some queues. Some of the functions have a ServiceBus trigger, but one has a Timer trigger, and one is manually executed via an HTTP POST. All of the messages we drop onto our queues have a correlation id that assists us with looking things up in our logs. While this is great for production troubleshooting, it also comes in handy during development.

While testing my functions, I don’t necessarily want to fire up the larger application, click through several screens, set up data, and then click whatever button I need that generates the message for my queue, so instead, I have a sample payload in Vim that I copy and paste into the Azure Portal whenever I need to post the message to one of our queues.

Here’s a made up json payload:

{
  "CorrelationId": "e961052d-65af-4b5a-9551-542bdc263d2d",
  "FooType": "Baz"
  "Foo": "Bar"
}

Because I want each of the messages to have a unique correlation id and not just keep using the same one over and over, I was originally using the “Generate GUID” tool within Visual Studio. If you’re not familiar with Visual Studio’s Create GUID tool, it’s about half-way down on the Tools menu. This is what the tool looks like. It’s relatively simple and to the point.

While I used it for a while, it quickly became a burden because of the clicks, and I wanted an easier method. I had actually started my own utility which was going to be a couple lines of C#, but I knew this had to be a solved problem, so I searched first.

TIL

If you’ve read any of my old “TIL” posts, you’ll know I love the terminal, so I learned of two ways to generate GUIDs from the command line: one in my Mac terminal and one from PowerShell in Windows.

Updates

Start of the update

Shortly after publishing this post, I received this question on Mastodon

My response was simple: when I wrote this, I had no idea about the cmdlet.

After a little exploration and reading, it does look like you should use the New-Guid cmdlet instead of what I used initially!

First, in PowerShell, creating a GUID is pretty freakin’ simple:

OLD WAY!

NEW WAY!

Back in TIL-7, I talked about using the Clipboard from PowerShell, so in the end, this is the command I run when doing the work I described above!

OLD WAY!

[guid]::NewGuid() | Set-Clipboard

NEW WAY!

New-Guid | Set-Clipboard

Thanks, Tom! I appreciate that you read the post and asked the question!

End of the update

The output is placed directly in the clipboard so now I can drop it into the json snippet I have in Vim. Easy.

When I’m on my Mac, I’m not running PowerShell, but I still want to easily generate GUIDs in the same fashion. It’s not much different!

uuidgen | pbcopy

Both of those commands are in my command buffer, so all I ever need to do is start typing the command and then right-arrow to complete it before hitting enter to snag a new GUID.

Bonus TIL

By the way, on the Mac, if I want to paste whatever is in the clipboard from the terminal, I can do something like this:

pbpaste > guid.txt

and now I have a text file with the guid I just generated and placed in the clipboard.

If there is a document in the clipboard, I can paste it to another location using the same command:

pbpaste > ~/Documents/MyDocument.txt

I hope you find this tip handy! It’s not something I use every day, but it’s nice for the times I need to easily generate GUIDs.


A seal indicating this page was written by a human

Updated:

Comments