Making your first game in Powershell
Have you ever sat around bored at work and wondered “What kind of fun or helpful powershell script can I make today?” I was a systems administrator at an old position and automated nearly anything I could think of. During my lunch break I saw my coworker playing a Russian roulette style game on his phone and thought to myself “Hmm maybe i can create something similar myself” and thats how I came up with this script.
How it works
It starts with the write-host command which displays the scripts title in yellow. Then I set a while loop to always equal true(1 -eq 1) to keep the game running until we decide we no longer want to play.
The $Pull variable grabs the users input if we want to pull the trigger.
For the $Spin variable and used the Get-Random command to randomly choose a number between 1-6
Then I set the bullet to equal 2 in a if statement. So if $Spin equals 2, you die.
After dying there is another if else statement asking if you’d like to play again. If the player decides not to, the loop breaks and closes the script.
If you were lucky enough to not get killed after pulling the trigger the first time, the $Counter keeps track of how many rounds you survived and displays it to the user. It also resets when you die as well.
Feel free to leave a message in the comments if you run into any issues with the script or have any recommendations.
Interested in learning powershell?
Check out “Mastering Windows PowerShell Scripting – Second Edition: One-stop guide to automating administrative tasks“. This comprehensive guide starts from scratch and covers advanced-level topics to make you a PowerShell expert. The first module, PowerShell Fundamentals, begins with new features, installing PowerShell on Linux, working with parameters and objects, and also how you can work with .NET classes from within PowerShell.
Download the script on github
This post is located under the Programming Category