Obtaining Unique IDs with PowerShell

I’m currently working on a project where I have a bunch of images which will be referenced and displayed using a RESTful MVC application. Something like this would be my first choice:


MyUrl.com/image/2 and so on

Pretty standard stuff, but whilst it’s nice for me to know the id of the actual record I am displaying, it also makes it super trivial for someone to scrape my whole website or know how many images I have in total. Perhaps that’s a sensitive trade secret. Or perhaps not in my situation, but in any case, I don’t want to make this too easy.

So, I need some kind of unique ID but one which doesn’t lend itself to an incremental algorithm. Finally, whilst I could have used numbers, I don’t really want to remember what the next one is and have to manage all that. My choice was therefore to use GUIDs and I don’t mean the Scottish form of food!

What are GUIDs?

GUIDs are also known as globally unique identifiers meaning that you can be pretty sure they are unique within the space of our planet – is that unique enough for you? Interestingly, there are also things called UUIDs – Universally Unique Identifiers which are unique in the universe, believe it or not!

Size wise, they are 128 bits in length which means there are 2^128 different combinations – that means, 2 to the power of 128. As an example, just 2^8 gives you 256 different combinations and from there it just gets MASSIVE really, really quickly.

Here’s an example of one to whet your appetite:


No-one’s going to be guessing that in a hurry and if you look at the next one I have generated:


that becomes abundantly clear.

In terms of format, you can see that they are presented as 8 alpha-numerics, followed by a grouping of 4, 4 and another 4, finished off with a final splodge of 12 characters, all separated with dashes.

But how do we do it in PowerShell?

Making a GUID in PowerShell

Type the above two lines into a PowerShell session and you will get a result much like I had. That looks pretty good but I’m not so keen on those dashes – I’d rather it was just a string of numbers and letters. Let’s get rid of those separators next:

And the output:


Looking better. It would be even nicer if I had a bit of control over the size of the resulting string – maybe, for instance, I just want 10 characters. Let’s try that:

And the output is:


Perfect. One last thing, let’s wrap this up into a function so that you can paste it into your projects:

The Final Function

And to call it, do something like this:

Back to my URLs? Now I have things like this:


One last thing – am I hurting my uniqueness by only taking the 10 instead of the whole string? Yes, definitely, but things will be fine. Let’s look at the maths.

For each position, there are 16 choices: 0-9 and A-F, which constitute a hexadecimal number. Since I have 10 of them, that means my address space (the total number of different combination I could have) is: 16^10. Plonk that into calculator and we get: 1,099,511,627,776. I’ll be OK 🙂

Small Print: Please don’t mention page scraping and link following; I’m pretending they don’t exist 😉


Written by Stephen Moon
email: stephen at logicalmoon.com
www: https://www.logicalmoon.com

Hey! Did you enjoy reading this? If you did and would like an email when I add new content, just subscribe to my list. You can unsubscribe at any time.

One thought on “Obtaining Unique IDs with PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *