Basic PowerShell Script with Menu

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.