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/1



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:

5a11b3c8-2a22-4a81-bd18-3f31e2aae456

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

4cc99e72-e573-40f8-bef7-c928d5774512

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

1
2
$g = [guid]::NewGuid()
$g

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:

1
2
3
4
$g = [guid]::NewGuid()
$v = [string]$g
$v = $v.Replace("-", "")
$v

And the output:

87af9b33122143dc83daee21974bed10

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:

1
2
3
4
$g = [guid]::NewGuid()
$v = [string]$g
$v = $v.Replace("-", "")
$v = $v.substring(0, 10)

And the output is:

64b9da56f8

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

The Final Function

1
2
3
4
5
6
7
function GetNextID($maxSize = 10)
{
    $g = [guid]::NewGuid()
    $v = [string]$g
    $v = $v.Replace("-", "")
    return $v.substring(0, $maxSize)
}

And to call it, do something like this:

1
GetNextID 10

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

MyUrl.com/image/4cc99e72e5

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 ;-)


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com