How to make a game using PowerShell Old School Style – Dragon Slayer

I love messing around from time to time and recently I’ve been working on a new book which I’ll be releasing on Amazon which focuses on writing fun games using PowerShell! If you want to keep up to date on its development you should subscribe to the notification list here. Back in the day I spent many hours writing ASCII based games at school and college and I wanted to give it a try again only this time put together a book which teaches PowerShell scripting in a fun way.

I’m going to share one of the lessons from the book with you now, its a text based role-play game where you have to make decisions based on the story line. Your decisions will directly effect the outcome of any future decisions made, so in order to put this together we need to learn a few basics first.

  • Controlling the PowerShell host (Resizing the window if its running as a command prompt)
  • Displaying text on the screen
  • Reading characters from the keyboard
  • Storing and reading information in variables
  • Structuring code so its easy to manage and read

Let’s start by writing a method we can call to set the size of the PowerShell window to known values. We need to do this so our title screens, menu’s and levels display correctly when run on other machines. Here I setup a function called Setup-Display() which sets the width and height of the PowerShell window to known values, in this case 55 characters top to bottom and 110 characters from left to right.

function Setup-Display()
$phost   = get-host
$pwindow = $phost.ui.rawui
$newsize = $pwindow.windowsize
$newsize.height = 55
$newsize.width  = 110
$pwindow.windowsize = $newsize

Once we have called the Setup-Display() method we can call our second method Title-Screen which obviously displays the title screen of the game. This is a cut down version of the code but don’t worry you can download the whole script at the end of this post.

There are several key parts to this method, first is the cmdlet Clear-Host which wipes everything from the window. If someone runs our script after running other commands then it wont display correctly so clear everything from the window using Clear-Host.

Next we can use the Write-Host cmdlet to display text on a new line in the window. Here we ask the user to press any key to play or  press q to quit then next we create a variable $Continue which saves the key the user presses. Here we call ReadKey which waits for the user to press a key which then returns a key press object which we save into the $continue variable. Finally we use an if statement and compare the Character value to the q character. If they match then we set a global variable runGame to $false which stops the game. You will see from the code download that global variables are stored at the top of the script and are prefixed with $global: which simply states that they can be used anywhere within our script where as other variables we use such as $continue are created within a method and can only be used within that method and not called from another method later on.

function Title-Screen()

Write-Host “Press Any Key to Play or Q to quit”

$continue = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

if($continue.Character -like ‘q’)
$global:runGame = $false
Using these simple PowerShell cmdlets you can create a really complicated and in depth role playing adventure game and reminisce of the good old days! Download the code, study it, modify it and experiment with your own changes.

You can download the source code here on my Github page: https://github.com/slashadminsource/DragonSlayer






PowerShell isn’t just an administration tool it can be fun too! Download the code and expand on this example and if you create something amazing send it to me and I’ll post it here for others to play! Maybe we should have a games programming competition!? Comment below if you would be interested.

Author: Ian@SlashAdmin

Share This Post On
468 ad


  1. When I press play on the script in ISE it immediately clears the screen. I’m making my own game and just past the title screen.

    In regular PS if I cd to the directory and .\game it doesnt start. It acts as if it clear-host’s after the title screen.

    Post a Reply
    • Yeah run it from a powershell window. It won’t run properly from ise. Call the script by typing .\game.ps1

      Post a Reply

Submit a Comment

Your email address will not be published.