Apps and Features PowerShell - Create a menu

information   Information
When making a PowerShell script to do multiple, optional tasks, it is practical to use a menu to let user to select what to do.

There are several methods to create a PS menu. In this tutorial, I will show how to create a simple menu using a so called Do - While loop, which performs tasks until user selects to quit script. In this sample, I will create a menu to either export (backup), delete, start or stop (shut down) one or more virtual machines using grid view table selection (see tutorial).

You can view and download the sample script on my OneDrive. Line numbers used in this tutorial refer to line numbers shown in OneDrive Preview.

View and Download: SampleMenu.ps1

1. To start with, we create a menu, and store it to variable $MainMenu:
$MainMenu = {
Write-Host " ***************************"
Write-Host " *           Menu          *"
Write-Host " ***************************"
Write-Host " 1.) Export virtual machines"
Write-Host " 2.) Delete virtual machines"
Write-Host " 3.) Start virtual machines"
Write-Host " 4.) Stop virtual machines"
Write-Host " 5.) Quit"
Write-Host " Select an option and press Enter: "  -nonewline

Screenshot showing how OneDrive Preview shows that part:

Create Menu.jpg
(Click to enlarge.)

How PowerShell will display it:

Sample Menu in PS.jpg

2. Syntax is $VariableName = {menu contents}, lines one through 13. Line 14 then clears PS window, when user has made a selection.

3. Next step is to create a Do - While loop, see lines 16 through 60 in OneDrive Preview:

Do {
Invoke-Command $MainMenu
$Select = Read-Host
Switch ($Select)
    1 {
        Commands to be run if user selects menu option 1
    2 {
        Commands to be run if user selects menu option 2
    3 {
        Commands to be run if user selects menu option 3
    4 {
        Commands to be run if user selects menu option 4
While ($Select -ne 5)

See the OneDrive Preview for line numbers. Line 18 calls the menu, variable $MainMenu, and shows it. Line 19 sets menu option number user has selected to variable $Select. Line 20 swithes to selected option, executing its commands.

At the end, line 60 checks if user has selected menu option 5. If not (variable $Select not equals 5), menu will be shown again. If user has selected 5, script exists.

4. For each possible menu option, we need to write cmdlets and commands to be run if that option is selected. In this sample, there are 4 options to select, fifth being to exit script. Each option starts with a number corresponding to a menu selection, followed by list of cmdlets and commands to be executed.

Let's look at menu option 1 inside the Do - While loop, export virtual machines (lines 22 through 30 in OneDrive Preview). Line 25 is the most important, running a command to export selected virtual machines:

    1 {
       Write-Host " Select virtual machines to Export."
       Get-VM | Out-GridView -Title "Select virtual machines to export" -PassThru | Export-VM -Path H:\VMExport
       Write-Host " Selected virtual machines have been exported."

Syntax is X {commands to be run if user selects this option}, where X is the menu option number. At the end of command in line 25, -Path H:\VMExport refers to an existing folder in which virtual machines will be exported. In this sample, options 2, 3 and 4 do not require a path.

5. We repeat step 4 to create commands for other menu options, in this sample options 2, 3 and 4.

That's it.



  • PowerShell.png
    11.9 KB · Views: 72
Last edited by a moderator:


New Member
Local time
12:33 PM
As usual, Kari - genius.
Definitely an excellent guide but for the very advanced.
I have already entered my question here, but no one has answered yet:

Please describe step by step what to do to create a two-partition usb boot. In one Windows 10 and in the other Windows 11?


My Computer

System One

  • Operating System
    Win 10 a Win11