r/PowerShell • u/bland3020 • Jan 17 '26
I Built a D&D Character Generator and Learned PowerShell Can Be FAST
TL;DR
Through profiling and optimization, I took a PowerShell module from ~89ms cold start to 9 microseconds per character generation (warm). Replacing Get-Random with [System.Random]::Next() alone saved nearly 50ms.
The Project
I wanted to learn PowerShell classes, so I built a D&D 2024 character generator with full class inheritance (Humanoid → Species, DnDClass → Fighter/Wizard, etc.). It generates random characters with stats, backgrounds, skills, and special abilities.
Repository: https://github.com/archibaldburnsteel/PS-DnD2024-ToonFactory
Tools Used
The Profiler module from PSGallery was invaluable:
powershell
Install-Module Profiler
$result = Trace-Script { New-DnDCharacter } -Simple
$result.Top50SelfDuration
# See what's actually slowMy Process
- Profile to find the biggest bottleneck
- Refactor (usually replacing cmdlets with .NET)
- Profile again to measure impact
- Repeat
I probably spent a full day just optimizing, which wasn't necessary for a character generator, but I learned a ton about PowerShell performance.
Key Takeaways
- Cmdlets are convenient but costly - Great for interactive use, expensive in loops
- Profile before optimizing - I would've never guessed Get-Random was the bottleneck
- .NET APIs are your friend - Direct method calls are orders of magnitude faster
- PowerShell can be fast - With optimization, microsecond-scale performance is possible
Questions
- Are there other common cmdlets I should watch out for in performance-critical code?
- Did I miss any obvious optimizations? (Feedback welcome!)
- Has anyone else done similar profiling work? I'd love to see other examples.
Thanks for reading! This was my first real dive into PowerShell performance optimization and I wanted to share what I learned.