PowerShell: Create Menu Function

Aaaand we’re back. Today I’ll be showing you something that I been using a whole lot in any of my scripts that require human interaction. This is not a GUI, but a TUI (Text-based user interface). This is a perfect way to have options, or run a task. We want to give the user/tech a limited amount of options to avoid human errors (which makes the script a lot less complex), and also add some color (to spice things up). Write-Host cmdlet allows use to have the -ForegroundColor and -BackgroundColor

Function Menu{
$Title="Menu System"
Write-Host "================ $Title ================" -Fore Magenta

Write-Host "`n Menu System:" -Fore Cyan
[int]$global:Menu = 0
while ( $global:Menu -lt 1 -or $global:Menu -gt 4 ){
Write-host "1. Option 1" -Fore Cyan
Write-host "2. Option 2" -Fore Cyan
Write-host "3. Option 3" -Fore Cyan
Write-host "4. Option 4" -Fore Cyan
[Int]$global:Menu = read-host "Choose an option 1 to 4" }
Switch( $global:Menu ){
1{$Global:Option1 = Color}
2{$Global:Option2 = Color}
3{$Global:Option3 = Color}
4{$Global:Option4 = Color}
#Another Option can bee added below here.

Function Color{
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_}
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -BackgroundColor $_}

Now run out and make your very own TUI – it’s pretty fun!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s