This is an update to a previous blog which can be found here. I am now considering that one out of date and will leave a link to this one as, well, it’s better.

you can find the full code I used at the bottom of this blog.

I wanted to change my games and add some more functionality as I wanted everything to be in one place, in one file so that execution policies weren’t as big of an issue and also so that, if someone used my code, they wouldn’t have to change file locations within the scripts which can be a real pain.

Now, don’t get me wrong, this code is not perfect. For example, I am having difficulty verifying that the input data for the 1 to 100 guess game is actually a number, currently if anything but a number is entered it will output an error, but at least it carries on and doesn’t “blow up.”

The script can be broken up into 3 distinctive parts: the menu; the games and a function to change the PowerShell windows to a more convenient size. (Now in “Your Boss Probably Won’t See You Pissing About Size”).

Anyway, heres the code in full:

#Window size change
funtion change-gamewindowsize{

 $pshost = Get-Host
 $pswindow = $pshost.UI.RawUI
 $newsize = $pswindow.BufferSize
 $newsize.width = 150
 $pswindow.buffersize = $newsize
 $newsize = $pswindow.windowsize
 $newsize.width = 51
 $newsize.Height = 10
 $pswindow.windowsize = $newsize

#INTRO (Start Menu)
function menu2{

 function Show-Menu{
  param (
  [string]$Title = 'My Menu'
  Write-Host "================ $Title ================"

  Write-Host "1: Press '1' to play 1-10 with 1 guess"
  Write-Host "2: Press '2' for 1-100 with unlimited guesses"
  Write-Host "Q: Press 'Q' to quit."

 Show-Menu -Title 'Select an option'
 $selection = read-host "please make a selection"

 if ($selection -eq "1"){
  write-host "You chose option one" (single-guess)
 }elseif ($selection -eq "2"){
  Write-Host "you chose option two" (1to100guessgame)
 }elseif ($selection -eq "q"){
 Do {$areyousuremessage = Read-Host "Are you sure you want to quit? Y or N?"} while (("y","n") -notcontains $areyousuremessage)
 switch ($areyousuremessage){
  "y" {exit}
  "n" {menu2}

 }elseif ($selection -ne "1" -or "2" -or "q"){

#Single guess game
function single-guess{

 Write-Host "You only have one guess!"
 Start-Sleep -Seconds 1
 Write-Host "Are you ready?"
 start-sleep -Seconds 1

 $random =  Get-Random -Minimum 1 -Maximum 11

 do {
  try {
  $numOk = $true
  [int]$input1 = Read-host "Make a guess between 1 and 10"
  }catch {$numOK = $false}
 }until (($input1 -ge 1 -and $input1 -lt 11) -and $numOK)

 if ($input1 -eq $random){
  Write-Host "You got it right!"
 }elseif ($input1 -ne $random){
  Write-Host "You got it wrong! the number was $random"

 Start-Sleep -Seconds 2

 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
 }elseif ($playagain1 -eq "n"){


#1-100 Guess game
function 1to100guessgame{


 [int]$random100 = Get-Random -Minimum 1 -Maximum 101

 Write-Host "Try and guess my number between 1 and 100"

 while ($input100 -ne $random100){
  Write-Host "enter your guess below"
  $input100 = [int] (Read-Host)

  $option1 = if ($input100 -gt $random100) {Write-Host "$input100 was too high"}
  $option2 = if ($input100 -lt $random100) {Write-Host "$input100 was too low"}
  $option3 = if ($input100 -gt [int](100) -or $input -lt [int](0)) {Write-Host 'between 1 and 100!'}

 } Write-Host "Correct! the number was $input100"

 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

 if ($playagain1 -eq "y"){
 }elseif ($playagain1 -eq "n"){


Obviously, the code can be collapsed at each different function. And to add a new game you only need to create a new entry in the menu listing and then add the “Do you want to play again” bit at the end of it. Basically childsplay.

Have fun!

One thought on “Games in PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *

IT. Delinquent
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe to our
Stay up to date with the latest posts and news!