A script can provide an alternative command line interface to manage PowerShell commands. User-friendly menu simplifies the execution of functions and makes PowerShell tasks easy to manage.
As an alternative to complexities of command syntax this type of approach might be usefull to enhance PowerShell usage. We know from various examples that such interfaces in NodeJS apps.12 And despite their modest appearance, they can be extremely effective and successful in some cases.
Overview
Tested on Windows 10 Enterprise
with PSVersion 5.1.19041.3031
, this script provides a reliable and efficient solution for managing commands.
I also used ChatGPT to improve the original script3. Since I think this is also a practice of using chatgpt I also wanted to share improvements it made.
You can save it to a file named like test.ps1
and run.
The content of script content is as follows:
Its noted that,
- Script allows nesting as many menus deep as we need and we can put
our code
to execute at any level. pause
is put when the code executes waiting for a user to press any key.- If you don’t have that in there, the script will clear the console and go back to the menu after your code executes.
- So that we can review the output before returning to the menu system.
- Script work well but switching to
WinForms
might be also an option.
ChatGPT Improvements
- Renamed functions to follow PowerShell’s verb-noun naming convention for consistency and clarity.
- Consolidated the menu display code into separate functions for better organization.
- Added a new function, Execute-Task, to handle task execution based on user input, with better error handling for invalid selections.
- Utilized a switch statement instead of multiple if statements for better readability and maintainability.
- Incorporated a do-while loop to continuously display the main menu and allow the user to navigate through submenus until they choose to exit.
- Added validation to display an error message for invalid selections.
- Implemented parameterization for the Execute-Task function to pass the user’s selection as a parameter.
References
-
Nodeschool has an interactive tool works like that. This application, which is also a NodeJs package, aims to teach nodejs. ↩
-
Powershell Multi-choice Menu and Sub menu - Stack Overflow ↩