Rock, Paper, Scissors In PowerShell

Another game today, I’ll be adding this to my big Games Project soon. Just need to implement it into the script.

First I started off by making the PowerShell window the correct size. I did this by adding:

$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 500
$newsize.width = 500
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = 22
$newsize.width = 85
$pswindow.windowsize = $newsize
$pswindow.WindowTitle = "Rock, Paper, Scissors"

to the start of the function.

Next I created the meat of the game, here its just a simple do statement until either the score of the user or the score of the computer has reached or exceeded 3. Best of three sort of thing. Heres that part:

do {
 $options = @("Rock","Paper","Scissors")
 $option = $options | Get-Random -Count 1
 Clear-Host
 Write-Host "Your Score - $score" -ForegroundColor Green
 Write-Host "Computer's Score - $computerscore" -ForegroundColor Red
 do {$selection = Read-Host "Selection"}while (("Rock","Paper","Scissors") -notcontains $selection)
  if ($selection -eq "rock" -and $option -eq "paper"){
   Write-Host "You lose, the computer chose $option";
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "paper" -and $option -eq "scissors"){
   Write-Host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "scissors" -and $option -eq "rock"){
   write-host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif ($selection -eq $option){
   Write-Host "Its a draw"
   Start-Sleep -Seconds 1
  }else{
   Write-Host "You won, the computer chose $option"
   Start-Sleep -Seconds 1
   $score = $score + 1
  }
 }until (($score -eq 3) -or ($computerscore -eq 3))
 if ($computerscore -gt $score){
  Clear-Host
  Write-Host "You lost, the computer scored $computerscore points whilst you only scored $score points" -ForegroundColor Red
 }else{
  Clear-Host
  Write-Host "You won, you got $score points. More than the computer's $computerscore" -ForegroundColor Green
 }
}

Finally, I ended the code of by showing the user who won and giving then the option to exit the game or play again. Here is the code for that:

do {$playagain = Read-Host "Do you want to play again? Y or N"} while (("Y","N") -notcontains $playagain)
if ($playagain -eq "y"){
 RPS
}elseif ($playagain -eq "n"){
 exit
}

===================================================================================================

Hopefully you can see how this all comes together. Here is the entire script for the game:

function RPS{
 $pshost = get-host
 $pswindow = $pshost.ui.rawui
 $newsize = $pswindow.buffersize
 $newsize.height = 500
 $newsize.width = 500
 $pswindow.buffersize = $newsize
 $newsize = $pswindow.windowsize
 $newsize.height = 22
 $newsize.width = 85
 $pswindow.windowsize = $newsize
 $pswindow.WindowTitle = "Rock, Paper, Scissors"

 [int]$score = 0
 [int]$computerscore = 0

 do {
  $options = @("Rock","Paper","Scissors")
  $option = $options | Get-Random -Count 1
  Clear-Host
  Write-Host "Your Score - $score" -ForegroundColor Green
  Write-Host "Computer's Score - $computerscore" -ForegroundColor Red
  do {$selection = Read-Host "Selection"}while (("Rock","Paper","Scissors") -notcontains $selection)
  if ($selection -eq "rock" -and $option -eq "paper"){
   Write-Host "You lose, the computer chose $option";
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "paper" -and $option -eq "scissors"){
   Write-Host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif($selection -eq "scissors" -and $option -eq "rock"){
   write-host "You lose, the computer chose $option"
   Start-Sleep -Seconds 1
   $computerscore = $computerscore + 1
  }elseif ($selection -eq $option){
   Write-Host "Its a draw"
   Start-Sleep -Seconds 1
  }else{
   Write-Host "You won, the computer chose $option"
   Start-Sleep -Seconds 1
   $score = $score + 1
  }
 }until (($score -eq 3) -or ($computerscore -eq 3)) 
 if ($computerscore -gt $score){
  Clear-Host
  Write-Host "You lost, the computer scored $computerscore points whilst you only scored $score points" -ForegroundColor Red
 }else{
  Clear-Host
  Write-Host "You won, you got $score points. More than the computer's $computerscore" -ForegroundColor Green
 }

 do {$playagain = Read-Host "Do you want to play again? Y or N"} while (("Y","N") -notcontains $playagain)
 if ($playagain -eq "y"){
  RPS
 }elseif ($playagain -eq "n"){
  exit
 }
}

I was quite surprised at how short I got the script to be, I think I may try to make the Blackjake game shorter as, lets me honest. Its barbaric in its construction.

Games In PowerShell – Version 4

Quite a large update to my Games in PowerShell project, adding a new game, changing some of the styling of the game and changing the loading screen of the game.

You’ll be pleased and proud to know that the games code is now over a thousand lines long…At least fake being proud then.

As mentioned earlier, I have added a new game. This one is different from my other games because it is a text based adventure game about a dino that needs capturing. The player only successfully completes the game once they have captured the dino and also not been killed by it. Tall order, I know.

I also changed how the Quick Fire Character Counting game works. It now starts by giving the player a full second to see the word and guess how many characters it contains, then, each time the user gets a correct answer reduces the viewing time of the word by 30 miliseconds. That way the game gradually gets more difficult instead of being stuck at the same length, which is boring.

Here is the link to my code for the game

I will be updating my project page with this update as well. Enjoy!

Verb Guessing Game in PowerShell

Verb guesser Featured image

Another game today. In PowerShell of all things. Who would have thought?

Anyway, in this blog post I will be showing you the code I used to create a random word guessing in PowerShell. The script works by taking all the verb information from the

Get-Verb

command and selecting only the verb part of each one.

The script then selects on at random, does some formatting and removes the vowels. That part is important. Pretty much is the single defining feature that makes this thing a game. With the vowels now removed, the user has to guess the verb. If they get it correct then a point is added to their score. If they get it wrong then the game resets and they start again from 0 points. (Well, 1 point if you’re being technical)

Here the code:

function verbguessinggame{
 $getverbs = Get-Verb | select verb
 [int]$verbamount = $getverbs.count
 $score = 0
 do{
  $random = Get-Random -Minimum 0 -Maximum $verbamount
  $selectedword = $getverbs[$random] -replace "@{verb=","" -replace "}",""
  $selectedwordlength = $selectedword.length
  $vowelsremoved = $selectedword -replace '[aeiou]',""

  $score = $score + 1
  Clear-Host
  Write-Host $vowelsremoved -ForegroundColor Red -BackgroundColor Black
  $guess = Read-Host "What is the word?"
 }until($guess -ne $selectedword)
 Clear-Host
 Write-Host "incorrect, the verb was $selectedword"
 Write-Host "Your score was $score"
 Start-Sleep -Seconds 1.5
 Clear-Host

 do {$playagainselection = Read-Host "Do you want to play again? (Y or N)"} while (("y","n") -notcontains $playagainselection)
 if ($playagainselection -eq "y"){
  verbguessinggame
 }elseif ($playagainselection -eq "n"){
  exit
 }
}

verbguessinggame

I will be updating my Games in PowerShell project with this game as well as making some custom changes to this game and others in the project.

Quick Fire Character Counting Game In PowerShell

Another pointless activity in PowerShell. The premis of this game is that you have a short amount of time to look at a word, you then have to guess how many letters are in the word.

If you get the number correct, the game continues and your score increases. If you input and incorrect number, then the game ends, shows your score and exits.

As always I have added this to my “Games In PowerShell” project. Below is the code for the game:

function quickfirecounting{
 $getverbs = Get-Verb | select verb
 [int]$verbamount = $getverbs.count
 $scoreforquickfire = 0
 do{
  $random = Get-Random -Minimum 0 -Maximum $verbamount
  $selectedword = $getverbs[$random] -replace "@{verb=","" -replace "}",""
  $selectedwordlength = $selectedword.Length

  $scoreforquickfire = $scoreforquickfire + 1
  Clear-Host
  Write-Host "$selectedword"
  Start-Sleep -Milliseconds 550
  Clear-Host
  $guess = Read-Host "how many letters are in the word?"
 }until ($guess -ne $selectedwordlength)
 Clear-Host
 Write-Host "Incorrect! There were $selectedwordlength letters"
 Start-Sleep -Seconds 1
 Clear-Host
 Write-Host "Your score was $scoreforquickfire"
 Start-Sleep -Seconds 1
 Clear-Host

 do {$playagainselection = Read-Host "do you want to play again? (Y or N)"} while (("y","n") -notcontains $playagainselection)
 if ($playagainselection -eq "y"){
  ####GO TO GAME FUNCTION TO PLAY AGAIN####
 }elseif ($playagainselection -eq "n"){
  ####GO TO THE MAIN MENU####
 }
}

As you can probably see, I am using the verb section of PowerShell to get the random words. This is better and easier than inputting my own words individually.

Enjoy!

My Custom, Auto-Loading PowerShell Scripts

Wanted to add onto a post I made about 10 days ago about creating a custom PowerShell environment. I currently have 3 custom commands for user management, 2 custom commands for group management and 1 command to list these (and the “homepage”) for my PowerShell prompt. So for my first example below, in the custom PowerShell prompt I would use “list-users”.

Users

I suppose I will kick off an just start with my custom commands for user management. By the way, if you didn’t know the name of the function in the PowerShell custom command is the actual commandlet you use in the custom PowerShell prompt.

First of all, I wanted a simple and quick way of getting the all users that are enabled and sorting them, then outputting them to the OGV. The following is what I used:

function list-users{
 Get-ADUser -Filter {enabled -eq $true} | sort | select name, samaccountname | ogv
}

Nice and simple, right…?

Next, I wanted to get the actual location within Active Directory as to where the user account is stored. I also wanted to be able to use a username or a full name in order to search for this information. You can see my code below:

function list-userlocation{

 function list-useragainstusername{
  $username = Read-Host "Input a username"

  $checkingAD = Get-ADUser -LDAPFilter "(samaccountname=$username)"
  if ($checkingAD -eq $null){
   Write-Host "$username does not exist!"
   pause
  }else{Get-ADUser -Identity $username | select distinguishedname }
 }

 function list-useragainstfullname{
  $fullname = read-host "input a full name"
  $checkingAD = get-aduser -ldapfilter  "(name=$fullname)"
  if ($checkingad -eq $null){
   write-host "$fullname does not exist!"
   pause
  }else{get-aduser -ldapfilter "(name=$fullname)" | select Distinguishedname}
 }

 do {$selection = read-host "check against full name or username (F or U)?"} while (("f","u") -notcontains $selection)
 if ($selection -eq "f"){
  list-useragainstfullname
 }elseif ($selection -eq "u"){
  list-useragainstusername
 }
}

Just in case you want to know, the output for the above command will look something like this “CN=NAME,OU=First OU,OU=Second OU,DC=sanderson,DC=lan”.

Finally, I have a command which allows me to get the the group membership of a user and then output that to a file. I have also made this one so that I can use the full name of a user as well as their username.

function list-usermembership{

 function list-usermembershipfromusername{
  $username = read-host "Input a username"

  $checkingAD = Get-ADUser -LDAPFilter "(samaccountname=$username)"
  if ($checkingAD -eq $null){
   Write-Host "$username does not exist!"
   pause
  }else{
   Write-Host "File with membership has been output to the desktop"
   Get-ADPrincipalGroupMembership $username | sort | select name | Out-File -FilePath  "c:\users\YOU!\desktop\$username Group Membership List.txt" -Append
  }
 }

 function list-usermembershipfromfullname{
  $fullname = Read-Host "Input a fullname"

  $checkingAD = Get-ADUser -LDAPFilter "(name=$fullname)"
  if ($checkingAD -eq $null){
   write-host "$fullname does not exist!"
   pause
  }else{
   $fullnameresolved = Get-ADUser -LDAPFilter "(name=$fullname)";
   $filename = $fullnameresolved.SamAccountName;
   Get-ADPrincipalGroupMembership -Identity $fullnameresolved | sort | select name | Out-File "c:\users\YOU!\desktop\$filename Group Membership List.txt" -Append;
   Write-Host "file with membership has been output to the desktop"
  }
 }

 do {$selection = Read-Host "Do you want to use fullnames or usernames? (F or U)"} while (("F","u") -notcontains $selection)
 if($selection -eq "f"){
  list-usermembershipfromfullname
 }elseif ($selection -eq "u"){
  list-usermembershipfromusername
 }else {}

}

Again, just in case you wanted to know, this outputs to a text document that will roughly ressemble the following:

name

—–

Group #1

Group #2

This allows me to quickly see if multiple users are part of a group and also to get a reference of group membership before disabling a user and removing them from all of their groups.

Groups

Lets move onto group management automation. This area is a little less sparse because, well… in my experience atleast, users are dumber than groups.

Again, a very simple one to start off with. This one simply lists all of the groups on the domain, selects certain attributes of the groups, sorts them and then ouputs them to OGV.

Below is the code for that:

function list-groups{

 Get-ADGroup -Filter * | select distinguishedname, name | sort | ogv

}

As I said, the group side of things is a little sparse. As in I only have two custom commands for them.

My other custom command for Active Directory groups collects me the membership information for that group. Like the users, I also make sure that my input matches a group within AD. Below is my code:

function list-groupmembership{
 $groupname = Read-Host "What group do you want to check?"

 $adlistforgroupcheck = Get-ADGroup -LDAPFilter "(name=$groupname)"

 if ($adlistforgroupcheck -eq $null){
  Write-Host "$groupname does not exist"
  pause
 }else {
  Get-ADGroupMember -Identity $groupname | select name, samaccountname | sort name | ogv
 }
}

Re-Displaying the Custom Commands

Now, an issue I ran into when I added these to my PowerShell environment was that when I had ran a command, or used clear-host or something along those lines. It meant that I could no longer see my custom commands, making them useless since I couldn’t remember what I had called the bloody things 🙂

That’s why I create a new command that would imitate what the prompt looks like when I first load it up. You can see my code below:

function list-customcommands{

 write-host @"
Custom PowerShell Environment Loaded
Go to '$profile' for config changes
go to 'documents\windowspowershell\scripts\autoload to add new scripts'

List of commands:             |
USERS                    |   GROUPS
List-users | ogv            |   list-groups | ogv
list-usermembership | file to desktop     |   list-groupmembership | ogv
list-userlocation            |
|
"@

}

Bit of a long post I know, but necessary background on some examples for custom PowerShell environments and Active Directory automation.

Enjoy!

Custom PowerShell Environment and Modules

To add custom PowerShell modules to your PowerShell environment, you first need to find out where you PowerShell profile is. You can do this by typing in:

$profile

into a PowerShell prompt. It should look like the following:

$profile

Now we need to test if the path actually exists. To do this type:

Test-Path $profile

If the prompt returns $true, your good. If it returns $false then you will need to run the following command:

New-Item -Path $profile -Itemtype file -Force

Now that is done we should be able to open the file in notepad. You can either browse to it following the path in $profile or type in:

notepad $profile

This should open a blank text file:

$profile empty

In here is where you specify PowerShell to look for custom modules and can even add text to the PowerShell prompt. For example, if I had the following to the notepad, it will also be displayed when I open a new PowerShell window:

custom $profile (write-host)

But we can also use this to load custom functions into our PowerShell environment. To do this, got to the $profile location, here you should find a folder called “Scripts”

Scripts folder

Go into the folder and create a new folder, mine is called “autoload”

autoload

Here is where you create your custom functions/ scripts. For example, here is what I have:

list-example

where each file contains a single function.

Now that were done with the easy part. You want to go back into your $profile notepad and add the following in order for PowerShell to load your customer functions:

$psdir="C:\users\YOU!\documents\windowspowershell\scripts\YOURSCRIPTFILENAME"

get-childitem "${psdir}\*.ps1" | %{.$_}

You can see from my file. I have multiple files to make for easier sorting of my functions. I also have a custom start screen to list all of my current commands so that I don’t forget them. You can see that below:

$profile example

Just so that you can see, this is what my PowerShell prompts look like:

My prompt

For a list on verbs and commands that you can use, visit the Microsoft website and forums. You DON’T want to use verbs or commands that are used else where or that have a unique purpose. Choose verbs that are different. Good luck. Enjoy!

More Games In PowerShell

This is, yet again, another improvement to a script I created earlier, you can find this here.

What I have done is added some new games (3 to be exact) and also created some unnecessary “fancy” bits such as a loading screen and a “Goodbye” exiting message. I have also changed the menu to be a single “echo” command instead of the multiple “write-host” which makes the code look cleaner.

The three games I have added are pretty much all the same game. I was getting sick of guessing games and since making actual games in PowerShell is VERY complex. I decided to make a math game. Specifically one that give the user a math question and the user has to work out the correct answer. An example would be “4 * ? = 12”. Where the user would have to work out “?”.

I made three difficulties of this, easy (1-10), medium (1-20) and hard (1-50). This means that the answers in the easy section are from 1 to 10, the answers in the medium are from 1 to 20 and so on…

I also added a function to change the size of the window to something more appropriate and changed the window title to something more…fun.

Anyway, below is the code for this. Unfortunately, I didn’t break the “300 lines of code” achievement I had set for myself:

function changescreensizeforgames{
 $pshost = get-host
 $pswindow = $pshost.ui.rawui
 $newsize = $pswindow.buffersize
 $newsize.height = 500
 $newsize.width = 500
 $pswindow.buffersize = $newsize
 $newsize = $pswindow.windowsize
 $newsize.height = 15
 $newsize.width = 51
 $pswindow.windowsize = $newsize
 $pswindow.WindowTitle = "Games Galore!"
}
changescreensizeforgames

function fakeloadingbar{
 cls
 echo "-....."
 Start-Sleep -Milliseconds 150
 cls
 echo ".-...."
 Start-Sleep -Milliseconds 150
 cls
 echo "..-..."
 Start-Sleep -Milliseconds 150
 cls
 echo "...-.."
 Start-Sleep -Milliseconds 150
 cls
 echo "....-."
 Start-Sleep -Milliseconds 150
 cls
 echo ".....-"
 Start-Sleep -Milliseconds 150
 cls
 echo "....-."
 Start-Sleep -Milliseconds 150
 cls
 echo "...-.."
 Start-Sleep -Milliseconds 150
 cls
 echo "..-..."
 Start-Sleep -Milliseconds 150
 cls
 echo ".-...."
 Start-Sleep -Milliseconds 150
 cls
 echo "-....."
 Start-Sleep -Milliseconds 150
 cls
}
fakeloadingbar

function exitscreenforgames{
 cls
 echo @"
####   #    # #####  ##
#   #   #  #  #      ##
#####    ##   ####   ##
#    #   ##   #      ##
#    #   ##   #
#####    ##   #####  ##
"@
 Start-Sleep -Seconds 1
 exit
}

#Window size change

#INTRO (Start Menu)
function menu2{

function Show-Menu{
 param (
 [string]$Title = 'My Menu'
 )
 Clear-Host
 write-host @"
================ $Title ================

Guessing Games------------
1: Press '1' to play 1-10 with 1 guess
2: Press '2' for 1-100 with unlimited guesses

Multiplication Games------
3: Press '3' for easy mulitplication game (1-10)
4: Press '4' for medium multiplication game (1-20)
5: Press '5' for hard multiplication game (1-50)

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" (1to10guessgame)
 }elseif ($selection -eq "2"){
 Write-Host "you chose option two" (1to100guessgame)
 }elseif ($selection -eq "3"){
  write-host "You chose option three" (easymathgame)
 }elseif ($selection -eq "4"){
  write-host "You chose option four" (mediummathgame)
 }elseif ($selection -eq "5"){
  write-host "You chose option four" (hardmathgame)
 }elseif ($selection -eq "q"){
  clear-host
 Do {$areyousuremessage = Read-Host "Are you sure you want to quit? Y or N?"} while (("y","n") -notcontains $areyousuremessage)
  switch ($areyousuremessage){

   "y" {exitscreenforgames}
   "n" {menu2}

  }

 }else {
  (menu2)
 }
}

#Single guess game
function 1to10guessgame{

 clear-host
 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!"
 }else{
  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"){
  (1to10guessgame)
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }

}

#1-100 Guess game
function 1to100guessgame{

 clear-host

 [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"){
  (1to100guessgame)
 }elseif ($playagain1 -eq "n"){
  (menu2)
 }
}

#easy math game 1-10
function easymathgame{
 $easynewnumber1 = get-random -minimum 1 -maximum 11
 $easynewnumber2 = Get-Random -Minimum 1 -Maximum 11

 $easynumbermultiplied = $easynewnumber1 * $easynewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$easynewnumber2 * ? = $easynumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$easynewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($easynewnumberinput -ge 1 -and $easynewnumberinput -lt 11) -and $numOk)

 if ($easynewnumberinput -eq $easynewnumber1){
  Write-Host "You got it right!"
 }elseif ($easynewnumberinput -ne $easynewnumber1){
  Write-Host "You got it wrong, the answer was $easynewnumber1"
 } Start-Sleep -Seconds 1

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

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

#medium math game 1-20
function mediummathgame{ 
 $mediumnewnumber1 = get-random -minimum 1 -maximum 20
 $mediumnewnumber2 = Get-Random -Minimum 1 -Maximum 20

 $mediumnumbermultiplied = $mediumnewnumber1 * $mediumnewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$mediumnewnumber2 * ? = $mediumnumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$mediumnewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($mediumnewnumberinput -ge 1 -and $mediumnewnumberinput -lt 20) -and $numOk)

 if ($mediumnewnumberinput -eq $mediumnewnumber1){
  Write-Host "You got it right!"
 }elseif ($mediumnewnumberinput -ne $mediumnewnumber1){
  Write-Host "You got it wrong, the answer was $mediumnewnumber1"
 } Start-Sleep -Seconds 1
 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

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

#Difficult math game 1-50
function hardmathgame{
 $hardnewnumber1 = get-random -minimum 1 -maximum 50
 $hardnewnumber2 = Get-Random -Minimum 1 -Maximum 50

 $hardnumbermultiplied = $hardnewnumber1 * $hardnewnumber2
 Clear-Host
 Write-Host "Here is your question, can you work out '?'"
 write-host "$hardnewnumber2 * ? = $hardnumbermultiplied"

 do{
  try{
   $numOk = $true
   [int]$hardnewnumberinput = Read-Host "Enter your answer here..."
  }catch {$numOk = $false}
 }until (($hardnewnumberinput -ge 1 -and $hardnewnumberinput -lt 51) -and $numOk)

 if ($hardnewnumberinput -eq $hardnewnumber1){
  Write-Host "You got it right!"
 }elseif ($hardnewnumberinput -ne $hardnewnumber1){
  Write-Host "You got it wrong, the answer was $hardnewnumber1"
 } Start-Sleep -Seconds 1
 do {$playagain1 = Read-Host "Do you want to play again? Y or N"} while (("y","n") -notcontains $playagain1)

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

Hopefully you’re as happy with this code as I am, if you have any improvements please let me know 🙂

Enjoy!