Posted on November 17, 2015 · Posted in Powershell

Adding a Simple Colorful Menu to PowerShell Script

Let’s consider an example of making a simple colored menu for PowerShell allowing a user to easily select one of the available options of the script being executed. The script provides a user with several options to choose, control the selected option and perform the further actions depending on the choice.

Let’s assume that our simple script allows a user to start or stop a certain Windows service (wuauserv).

The list of menu options offered to a user can be displayed as follows:Write-Host '1. Start Windows Update service'
Write-Host '2. Stop Windows Update service'
Write-Host '3. Exit'

After that, prompt a user to select an option by typing its number:
$selected_menu_item = Read-Host 'Select menu item'

Then, process a user choice using “switch” operator:
1{net start wuauserv}
2{net stop wuauserv }
3{Write-Host 'Exit'; exit}
default {Write-Host 'Incorrect input' -ForegroundColor Red}

Run the script and check its health.

Simple menu in Powershell

Everything works well. The only thing is that the menu appearance leaves much to be desired. We would like to see something more elegant and convenient.

You can try to create a nicer menu with a colored heading, items and a frame manually but it is very time-consuming, as you will have to calculate table frame size manually depending on the length of text boxes. It is much easier to use a ready script. In Technet gallery, there is ready Create colorful PowerShell Menu Function (, with all necessary features. Save the function code to a file named color_menu.psm1 and import it into PoSh session:
Import-Module C:\PS\color_menu.psm1

To run the function of making a colored menu, do the following:

CreateMenu -Title "Windows Update Script" -MenuItems "Start Windows Update service","Stop Windows Update service","Exit" -TitleColor Red -LineColor Cyan -MenuItemColor Yellow

Colored menu in Powershell script

Thus, it took us about two minutes to create a nice colored menu for the PowerShell script. Both the creator and ordinary users can easily use such script in future.

Related Articles