Skip to content

Instantly share code, notes, and snippets.

@griffeth-barker
Last active June 1, 2025 14:18
Show Gist options
  • Save griffeth-barker/5183c8aedeaab1fe049538f37cd23092 to your computer and use it in GitHub Desktop.
Save griffeth-barker/5183c8aedeaab1fe049538f37cd23092 to your computer and use it in GitHub Desktop.
Ready to roll dice in PowerShell? Download the Invoke-DiceRoll function! It supports a parameter for each of the most common polyhedral dice (d4, d6, d8, d10, d12, 420), where you can provide an integer representing the quantity of that type of die you'd like to roll. You can roll multiple types of dice at the same time!
function Invoke-DiceRoll {
<#
.SYNOPSIS
Rolls one or more dice of standard types (d4, d6, d8, d10, d12, d20) and returns the results.
.DESCRIPTION
The Invoke-DiceRoll function simulates rolling dice commonly used in tabletop games.
You can specify the number of each die type to roll (d4, d6, d8, d10, d12, d20).
.PARAMETER D4
The number of 4-sided dice (d4) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.PARAMETER D6
The number of 6-sided dice (d6) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.PARAMETER D8
The number of 8-sided dice (d8) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.PARAMETER D10
The number of 10-sided dice (d10) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.PARAMETER D12
The number of 12-sided dice (d12) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.PARAMETER D20
The number of 20-sided dice (d20) to roll.
Default : 0
Required : false
ValueFromPipline : false
ParameterSetName : default
Type : integer
.EXAMPLE
Invoke-DiceRoll -D6 2 -D20 1
Rolls two (2) 6-sided dice and one (1) 20-sided die.
Sample output:
Die Unit RollValue
--- ---- ---------
d20 1 11
d6 1 5
d6 2 2
.OUTPUTS
[System.Object[]]
The object is comprised of a [System.Management.Automation.PSCustomObject] containing the Die type, Unit (roll number), and RollValue.
.NOTES
Author : Griff Barker ([email protected])
Date : 2025-05-20
#>
param(
[Parameter(ParameterSetName = 'Default')]
[int]$D4 = 0,
[Parameter(ParameterSetName = 'Default')]
[int]$D6 = 0,
[Parameter(ParameterSetName = 'Default')]
[int]$D8 = 0,
[Parameter(ParameterSetName = 'Default')]
[int]$D10 = 0,
[Parameter(ParameterSetName = 'Default')]
[int]$D12 = 0,
[Parameter(ParameterSetName = 'Default')]
[int]$D20 = 0
)
[hashtable]$diceTypes = @{
d4 = $D4
d6 = $D6
d8 = $D8
d10 = $D10
d12 = $D12
d20 = $D20
}
$rollResults = [System.Collections.Generic.List[psobject]]::new()
foreach ($dice in $diceTypes.Keys) {
[int]$count = $diceTypes[$dice]
[int]$sides = $dice.Substring(1)
for ($i = 1; $i -le $count; $i++) {
[int]$roll = Get-Random -Minimum 1 -Maximum ($sides + 1)
$rollResults.Add([PSCustomObject]@{
Die = $dice
Unit = $i
RollValue = $roll
})
}
}
Write-Output $rollResults
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment