Tim's Blog

Information, Technology, Security, and other stuff.

Basic PowerShell Script with Menu

Published 2017-03-24

I needed a Windows script that would let me perform a variety of options for deploying a piece of software faster with a little menu to pick which task to perform. In the past I would've written this as a batch file using goto statements to jump around for the different options I need, but now with PowerShell it can be a little more elegant:

Write-Host 'A Name for the Script'
$option = 0

function Get-Option {
  $option = Read-Host 'Enter 1 to install or 2 to configure'
  Execute-Option
}

function Execute-Option {
  if ($option -eq '1') {
    # Do something
  }
  if ($option -eq '2') {
    # Do something else
  }
  Get-Option
}

Get-Option

Once the application starts each function will just call each other until you Ctrl+C to kill it. Of course you can put an option of 'quit' or 'exit' in, but I chose to omit that here for the sake of simplicity.