Introduction
For this week’s Quick PowerShell Post of the Week we will cover a couple of practical uses of the Get-Random cmdlet. In the first example we will use it to generate some coupon codes to be used in a book promotion campaign and in the second example we will use it to generate some GUIDs for a script that needs to generate these values for an XML file. In the examples we will explore the different ways we can handle the generation of the random numbers as well as putting together multiple randomly generated numbers.
Get-Random Notes
First, the Get-Random cmdlet. There isn’t a lot to it. We have a few parameters which we can use to make the most of the cmdlet and we do NOT have to specify any options to generate a number:
The three options we have are as follows:
Minimum (or min): Generate a number that is greater than this.
Maximum (or max): Generate a number that is lesser than this.
SetSeed: Number used to initialize PowerShell’s random number generator.
Example One: Coupon Codes
For this sample we will generate a random coupon code which will be hard to guess. In order to do that we need to come up with some criteria for this exercise. This coupon code will be applied to only one book on the site, Exchange Online.
$Count = 0 Do { $GUID = Get-Random -min 1111111111 -Max 9999999999 Write-Host "ExO-2023-$GUID" $Count++ } While ($Count -lt 30)
Example Two: GUID Generator
While the first examples above are useful, note that both generate simple decimal numbers and no Hex numbers are actually generated. GUIDs sometimes contain letters, though not always. If we wanted to generate a version of the GUID based on Hexadecimal numbers, we can do that with a bit of tweaking. We have a couple of options to perform this action.
Option One
In the first option we will user the ‘-f’ operator which allows us to change the format of a string expression. Resource for this feature can be found here. Now onto the operator in production.
In this scenario we need to generate a GUID for a software application or DLP operation (XML file create .. Add link!). With the ‘-f’ operator we can create numbers with hex characters (0 .. F). First need to hexadecimal string formatter which is:
‘{0:X}’
Then we need to use Get-Random to generate the random character:
(Get-Random -Min 0 -Max 16)
Putting these together we get this one-liner
'{0:X}' -f (Get-Random -Min 0 -Max 15)
With no other code this will generate a number from 0 to F in hex: [two examples]
To generate a realistic GUID, we will use this as our format:
<8 char>-<4 char>-<4 char>-<4 char>-<12 char>
Using this format and the above expression, we can create the GUID by using this code below:
$GUIDs = @() $Count = 0 Do { $Char = '{0:X}' -f (Get-Random -Min 0 -Max 15) $GUIDs +=$Char $Count++ If ($Count -eq 8) {$GUIDS += "-"} If ($Count -eq 12) {$GUIDS += "-"} If ($Count -eq 16) {$GUIDS += "-"} If ($Count -eq 20) {$GUIDS += "-"} } While ($Count -lt 32) $GUID = $GUIDs -join '' $GUID
Couple sample run-throughs:
This provides us with useable GUIDs for our script.
Option Two
With this option we use a .Net Framework class to convert decimal to hex (base 16):
System.Convert
The first step is to generate a random number from 0 to 15 (see above) and then use the System.Convert code to convert that number into hex (base 16) as shown here:
$Num = Get-Random -Min 0 -Max 15 [System.convert]::ToString($Num,16)
These two lines can then be simplified like so:
[System.convert]::ToString($(Get-Random -Min 0 -Max 15),16)
We can replicate the code in Bonus Example 2 and perform a similar task.
$GUIDs = @() $Count = 0 Do { $Char = [System.convert]::ToString($(Get-Random -Min 0 -Max 15),16) $GUIDs +=$Char $Count++ If ($Count -eq 8) {$GUIDS += "-"} If ($Count -eq 12) {$GUIDS += "-"} If ($Count -eq 16) {$GUIDS += "-"} If ($Count -eq 20) {$GUIDS += "-"} } While ($Count -lt 32) $GUID = $GUIDs -join '' $GUID
Results of this code are as follows:
Secret
Some of my readers may be wondering why we bothered to go through all of this work for this and it’s simply to practice what can be done from within PowerShell. Luckily for us, an easier way is present as shown below:
[guid]::NewGuid()
Which can generate this for us:
With one change:
Thus we have an easier way to generate a GUID.
————————————————————————————————–
See previous Quick PowerShell Posts of the Week [ HERE ]
———————————————————————————————————–
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks:
Or just use New-Guid 😉
Thanks for your post(s), always nice to read.