I needed a fairly complication menu system in PowerShell so decided to create a PowerShell menu builder function that creates a menu prompt from variables passed to the function. This made the code much easier to read, separating the menu system code from the code that really does stuff.
Here’s the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# POWERSHELL MENU v1.0 # Menu system using functions # # AUTHOR: Andrew Morpeth # DATE: 2013 # VERSION: 1.0 # # This script is provided as-is, no warrenty is provided or implied.The author is NOT responsible for any damages or data loss that may occur # through the use of this script. Always test before using in a production environment. This script is free to use for both personal and # business use, however, it may not be sold or included as part of a package that is for sale. A Service Provider may include this script # as part of their service offering/best practices provided they only charge for their time to implement and support. # Function Menu ($MenuStart, $MenuEnd, $MenuName, $MenuFunctionName, $MenuOption1, $MenuFunction1, $MenuOption2, $MenuFunction2, $MenuOption3, $MenuFunction3, $MenuOption4, $MenuFunction4, $MenuOption5, $MenuFunction5, $MenuOption6, $MenuFunction6, $MenuOption7, $MenuFunction7, $MenuOption8, $MenuFunction8, $MenuOption9, $MenuFunction9, $MenuOption10, $MenuFunction10) { Write-Host “`n$MenuName`n” -Fore Magenta; if($MenuOption1 -ne $null){Write-Host “`t`t$MenuOption1” -Fore Green;} if($MenuOption2 -ne $null){Write-Host “`t`t$MenuOption2” -Fore Green;} if($MenuOption3 -ne $null){Write-Host “`t`t$MenuOption3” -Fore Green;} if($MenuOption4 -ne $null){Write-Host “`t`t$MenuOption4” -Fore Green;} if($MenuOption5 -ne $null){Write-Host “`t`t$MenuOption5” -Fore Green;} if($MenuOption6 -ne $null){Write-Host “`t`t$MenuOption6” -Fore Green;} if($MenuOption7 -ne $null){Write-Host “`t`t$MenuOption7” -Fore Green;} if($MenuOption8 -ne $null){Write-Host “`t`t$MenuOption8” -Fore Green;} if($MenuOption9 -ne $null){Write-Host “`t`t$MenuOption9” -Fore Green;} if($MenuOption10 -ne $null){Write-Host “`t`t$MenuOption9” -Fore Green;} [int]$MenuOption = Read-Host “`n`t`tPlease select an option” if(($MenuOption -lt $MenuStart) -or ($MenuOption -gt $MenuEnd)) { Write-Host “`t`nPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1 Invoke-Expression $MenuFunctionName } else { if($MenuOption -eq $MenuStart) {Invoke-Expression $MenuFunction1} if($MenuOption -eq ($MenuStart + "1")) {Invoke-Expression $MenuFunction2} if($MenuOption -eq ($MenuStart + "2")) {Invoke-Expression $MenuFunction3} if($MenuOption -eq ($MenuStart + "3")) {Invoke-Expression $MenuFunction4} if($MenuOption -eq ($MenuStart + "4")) {Invoke-Expression $MenuFunction5} if($MenuOption -eq ($MenuStart + "5")) {Invoke-Expression $MenuFunction6} if($MenuOption -eq ($MenuStart + "6")) {Invoke-Expression $MenuFunction7} if($MenuOption -eq ($MenuStart + "7")) {Invoke-Expression $MenuFunction8} if($MenuOption -eq ($MenuStart + "8")) {Invoke-Expression $MenuFunction9} if($MenuOption -eq ($MenuStart + "9")) {Invoke-Expression $MenuFunction10} } } #MENU OPTIONS: Space seperated list - #Menu option start number, Menu option end number, Menus descriptive name, Menus function name, #Menu option 1, Option 1 action (specify exisiting menu or other function), option 2, option 2 action and so on up to 10 options. # #E.G # function MENU_HOME #Wrap menu in function so that you can call it from anywhere to get back # { # Menu 1 3 "Menu Home" MENU_HOME "1. Go to menu home one" MENU_HOME_ONE "2. Go to menu home two" MENU_HOME_TWO "3. Do stuff" MENU_HOME_DOSTUFF #Invokes menu builder function and passes variables # } # MENU_HOME #Runs function # #Remember that powershell is processed line by line so functions need to be above where they are called. This gets trickier and trickier the more complicated your menu gets :) # ###MENU_HOME ############################################################################################################################################################################################################################### function MENU_HOME { Menu 1 3 "Menu Home" MENU_HOME "1. Go to menu home one" MENU_HOME_ONE "2. Go to menu home two" MENU_HOME_TWO "3. Do stuff" MENU_HOME_DOSTUFF } ###MENU_HOME_ONE ############################################################################################################################################################################################################################### function MENU_HOME_ONE { Menu 1 2 "Menu Home - One" MENU_HOME_ONE "1. Do stuff" MENU_HOME_DOSTUFF "2. Exit" MENU_HOME } ###MENU_HOME_TWO ############################################################################################################################################################################################################################### function MENU_HOME_TWO { Menu 1 2 "Menu Home - One" MENU_HOME_ONE "1. Do stuff" MENU_HOME_DOSTUFF "2. Exit" MENU_HOME } ###MENU_HOME_DOSTUFF ############################################################################################################################################################################################################################### function MENU_HOME_DOSTUFF { write-host "I like to do stuff!!" } MENU_HOME |
Hi Andrew,
Excellent work, thanks just what I was looking for. Will be making few changes to create menu to export mailboxes on Exchange.
Thanks,
No problem,glad it helped 🙂