Go to ...

RSS Feed

September 23, 2020

PowerShell – Create a simple menu


You can create menus for your PowerShell scripts in various ways. In this story, I will demonstrate one of the easiest methods. First, we create a menu as a variable, then, within a Do – While loop, we call the menu, let the user select an option, then perform whatever task(s) the user selected. Finally, the user returns to menu, until Quit is selected.

My sample menu is not very practical, it only creates either an MBR or GPT virtual hard disk file. But, for demonstrating how to make a PowerShell menu, it’s enough. You’ll get the point, and can create more complex menus by following the instructions.

I’ll break the menu script into smaller pieces. Let’s start with the menu itself:

$VHDType = {
Write-Host " *********************************"
Write-Host " *  Create a Virtual Hard Disk   *"
Write-Host " *********************************"
Write-Host " "
Write-Host " Select VHD type"
Write-Host
Write-Host " 1.) Create an MBR partitioned VHD"
Write-Host " 2.) Create a GPT partitioned VHD"
Write-Host " 3.) Quit"
Write-Host " "
Write-Host " Press 1, 2 or 3 and Enter to proceed: " -nonewline
}

The title image on top of this page shows how this menu looks when presented to user inside PowerShell.

Notice how the full menu is added as a value to the variable $VHDType, between the opening and closing curved brackets.

When the script is run, a Do – While loop is started, the console window is cleared, the menu is invoked, a few variables are set, and a possibly existing old DISKPART configuration script $VHDConfig is deleted:

Do {
cls
Invoke-Command $VHDType
$VHDLetter = 'W'
$VHDSize = '102400'
$VHDName = Join-Path $env:USERPROFILE '\Documents\W10.vhdx'
$VHDConfig = Join-Path $env:Temp '\VHDConfig.txt'
if (Test-Path $VHDConfig) {Remove-Item $VHDConfig}
$Select = Read-Host

Last, the user selection is written into the variable named $Select.

The PowerShell Switch command can be used instead of IF – THEN – ELSEIF – ELSE statements. Switch is especially practical in menus. Instead of requiring multiple IF – ELSEIF statements, we simply read the user’s selection, and proceed accordingly. In the preceding sample menu, that selection can be one of three integers: 1, 2 or 3. If the user selects 1, an MBR partitioned VHD is created:

Switch ($Select) {
1 {
$VHDName = Join-Path $env:USERPROFILE '\Documents\W10.vhdx'
if (Test-Path $VHDName) {Remove-Item $VHDName}
cls
Add-Content $VHDConfig "create vdisk file=$VHDName maximum=$VHDSize type=expandable"
Add-Content $VHDConfig "attach vdisk"
Add-Content $VHDConfig "create partition primary size=500"
Add-Content $VHDConfig "format quick fs=fat32 label=System"
Add-Content $VHDConfig "create partition primary"
Add-Content $VHDConfig "shrink minimum=500"
Add-Content $VHDConfig "format quick fs=ntfs label=Windows"
Add-Content $VHDConfig "assign letter=$VHDLetter"
Add-Content $VHDConfig "create partition primary"
Add-Content $VHDConfig "format quick fs=ntfs label=WinRE"
Add-Content $VHDConfig "set id=27"
Add-Content $VHDConfig "exit"
cmd /c diskpart /s $VHDConfig
Write-Host
Write-Host " An MBR partitioned VHD"$VHDName "created."
Write-Host
pause
cls
}

First, the script checks if the VHD with name given in variable $VHDName exists. If  it does, we will delete it. A DISKPART script will be created in the %TEMP% folder, and run with DISKPART. The user is shown a short message, and when Enter is pressed, returned to the menu.

If the user selects 2, the same is done but this time the selection creates a GPT VHD instead:

 2 {
if (Test-Path $VHDName) {Remove-Item $VHDName}
cls
Add-Content $VHDConfig "create vdisk file=$VHDName maximum=$VHDSize type=expandable"
Add-Content $VHDConfig "attach vdisk"
Add-Content $VHDConfig "convert gpt"
Add-Content $VHDConfig "create partition efi size=100"
Add-Content $VHDConfig "format quick fs=fat32 label=System"
Add-Content $VHDConfig "create partition msr size=128"
Add-Content $VHDConfig "create partition primary"
Add-Content $VHDConfig "shrink minimum=500"
Add-Content $VHDConfig "format quick fs=ntfs label=Windows"
Add-Content $VHDConfig "assign letter=$VHDLetter"
Add-Content $VHDConfig "create partition primary"
Add-Content $VHDConfig "format quick fs=ntfs label=WinRE"
Add-Content $VHDConfig "set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac"
Add-Content $VHDConfig "gpt attributes=0x8000000000000001"
Add-Content $VHDConfig "exit"
cmd /c diskpart /s $VHDConfig
Write-Host 
Write-Host " A GPT partitioned VHD"$VHDName "created."
Write-Host
pause
cls
}
}
}
While ($Select -ne 3)

Last but not least, the Do – While loop is set to always jump back to its beginning, as long as user does not select the value 3 for the variable named $Select (not equal, -ne 3).

To view and download the complete script, find it on OneDrive: MenuDemo.ps1

Please notice: This is a very basic script, only made to demonstrate how to create menus. It lacks remarks and error handling, just to point out two important aspects that are currently missing.

Kari

Author: Kari Finn

A former Windows Insider MVP, Kari started in computing in the mid 80’s writing code for VAX / VMS systems. Since then, he’s worked in a variety of IT positions. He specializes in Windows image capture, customization, repair and deployment as well as Hyper-V virtualization. Kari is a proud Team Member at number #1 Windows site TenForums.com.

Leave a Reply

More Stories From Kari's Blog