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{
#----------------------------------------------------------
#DYNAMIC VARIABLES
#----------------------------------------------------------
cls
$Title="Menu System"
Write-Host "================ $Title ================" -Fore Magenta

#TUI
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.
}
Menu

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!

Advertisements

PowerShell: Change Title Bar

I always see this common question, “How can I change the title of the Windows PowerShell console?”

Below you can see how we use the WindowTitle property from $host.ui.rawui.

$host.ui.RawUI.WindowTitle = “Changed Title”