Build a dynamic menu with Powershell

0Shares
Reading Time: 2 minutes

How to build a dynamic menu with Powershell

Do you have a million powershell scripts and navigate folder through folder to find the one you need and open them when required? Well this is the script for you. I was in a similar position, where I had 20+ scripts all over my computer and only opened them when they were of use.

My coworker came over one day and asked me showed him how one of my scripts worked, but the issue was i couldn’t find the exact one I needed. Thats when he suggested it would be a great idea to create a menu where i can access all my scripts from. That ladies and gentlemen is how this powershell script came to exist before your very own eyes below.



The Code


Download this powershell script on github.

 

How It works

The first line simply displays “Main Menu Choose an option” with Write-Host. That way If i missed my morning coffee and open the wrong thing, I’ll know what script I have open. The `n just makes a new line between the words. Think of it as the equivalent of pressing enter.

Next I used the Read-Host command, to the the user to enter an input and store it in a variable called $Option

This is where the real fun begins, I set a switch statement to take the $Option variable and if it matches a value, it executes the script block. For my example, if a user enters “1” the password reset script will open.

If a user enters a value that does not match, it will navigate to the default keyword at the end of the list and reopen the powershell script.

 

Interested In learning Powershell?

Check out “Mastering Windows PowerShell Scripting – Second Edition: One-stop guide to automating administrative tasks“. This comprehensive guide starts from scratch and covers advanced-level topics to make you a PowerShell expert. The first module, PowerShell Fundamentals, begins with new features, installing PowerShell on Linux, working with parameters and objects, and also how you can work with .NET classes from within PowerShell. Click the image below to purchase.

 

This post is located under the Programming Category

Learn how to create a Russian Roulette game written in Powershell!

0Shares

admin

Leave a Reply

Your email address will not be published.