{"componentChunkName":"component---src-templates-articles-index-jsx","path":"/articles/powershell-interactive-menu/","result":{"data":{"props":{"adsense":true,"adsenseMobile":null,"byLine":"","canonical":"https://staging.business.com/articles/powershell-interactive-menu/","content":"<p>Learn how to design and build your own interactive menus inside of your PowerShell scripts to help non-PowerShell users more easily navigate and use them.</p>\n<p><picture class=\"aligncenter full\">PowerShell scripts aren't generally written with interactivity in mind. Automation is all about hands-off activities, but there are times when you might need to write a script not for you but for others who might not know PowerShell like you do. You could write a GUI for your script but that can get complicated pretty quickly. Maybe you just need a way for a user to quickly provide some input to a script. To do this, you can build small interactive menus inside of a PowerShell script -- here's how.</picture></p>\r\n<h2>How to build an interactive menu<picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/bbc7d51ee47d96a8b4595/1200_-900- 1200w, /images/content/5b6/bbc7d51ee47d96a8b4595/668_-501- 668w, /images/content/5b6/bbc7d51ee47d96a8b4595/320_-240- 320w\" sizes=\"668px, 100%\"></source></picture>\n</h2>\r\n<p>There's a few ways to create an interactive menu in PowerShell. One way is to use a .NET object <a href=\"https://technet.microsoft.com/en-us/library/Ff730939.aspx\" target=\"_blank\">System.Management.Automation.Host.ChoiceDescription</a> and the PromptForChoice method. This is way is fine but I believe you have a little more control over the messaging if you design your own. This is what we'll be covering today.</p>\r\n<p>The menu that we'll be creating will be built using a single function called <strong>Show-Menu</strong> and a <strong>do/until loop</strong>. We'll include a few options to choose from and an option to quit from the menu at any time. The premise is to present the user with a few options, allow them to run those option and then present the menu again if they'd like to run other options.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/bbcd551ee47d76a8b4585/742_-189- 742w, /images/content/5b6/bbcd551ee47d76a8b4585/412_-105- 412w, /images/content/5b6/bbcd551ee47d76a8b4585/206_-52- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/bbcd551ee47d76a8b4585/418-107-\" alt=\"\" width=\"418\" height=\"107\" data-content-img-id=\"5b6bbcd551ee47d76a8b4585\"></picture></p>\r\n<p>To get started, you need a way to build this menu in a PowerShell function. This needs to be in a function because whenever the user selects an option and the script runs whatever is in that option, we need to show the menu again.</p>\r\n<p>Here is a very simple <strong>Show-Menu</strong> function. You'll notice that essentially it just does two things: Clears the host screen and then just writes some text to the screen. It needs to do nothing more. You could make this better by moving the options up as a parameter if you'd like, which would make it more reusable, but that's up to you.</p>\r\n<blockquote>\n<br>function Show-Menu<br> {<br>      param (<br>            [string]$Title = 'My Menu'<br>      )<br>      cls<br>      Write-Host \"================ $Title ================\"<br>      <br>      Write-Host \"1: Press '1' for this option.\"<br>      Write-Host \"2: Press '2' for this option.\"<br>      Write-Host \"3: Press '3' for this option.\"<br>      Write-Host \"Q: Press 'Q' to quit.\"<br> }<br><br>\n</blockquote>\r\n<p>Once the function is built, we'll then need a way to display it, provide user input and then, when whatever is going to happen actually happens when an option is chosen, we want to display the menu again. This is why we chose to use a do/until loop.</p>\r\n<p>A do/until loop executes code right off the bat. In this case, it shows the menu and then uses <strong>Read-Host</strong> to prompt for a selection.</p>\r\n<blockquote>\n<br>do<br>{<br>      Show-Menu<br>      $input = Read-Host \"Please make a selection\"<br>      switch ($input)<br>      {<br>            '1' {<br>                 cls<br>                 'You chose option #1'<br>            } '2' {<br>                 cls<br>                 'You chose option #2'<br>            } '3' {<br>                 cls<br>                 'You chose option #3'<br>            } 'q' {<br>                 return<br>            }<br>      }<br>      pause<br> }<br> until ($input -eq 'q')<br><br>\n</blockquote>\r\n<p>We're then using a switch statement to make a decision on what code to execute based on the option selected. This is where you might run another script, reboot a server, set a registry key, delete a file, etc.</p>\r\n<p>Also, always be sure to include a \"Q\" option. This is a way for the user to break out of the loop if they're done with the menu. And notice the pause keyword; this is to prevent the menu from displaying extremely quickly again, but when it isn't necessarily required.</p>\r\n<h2>Final step</h2>\r\n<p>Finally, the do loop will continually loop over and over again until the \"Q\" option. Once the \"Q\" option is selected, the until construct will be satisfied, which will then break out of the loop.</p>\n","docType":"article","guid":"articles_5e94a5eea486be4773f27c799589849f","lastModified":"2018-09-05T08:00:00.000Z","published":"2018-09-05T08:00:00.000Z","structuredData":null,"subtitle":"PowerShell scripts aren't generally written with interactivity in mind.","template":"v1","title":"How To Build An Interactive Menu Inside Of A PowerShell Script","type":"article","author":{"avatar":null,"bio":"Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.","name":"Adam Bertram","image":null,"slug":null,"social":{"facebook":"","linkedin":"https://www.linkedin.com/in/adambertram/","twitter":"https://twitter.com/adbertram","website":"https://www.adamtheautomator.com/"},"status":"enabled","title":null,"type":"Contributing Writer","user":{"imagePath":"https://staging.business.com/images/undefined","profile":{"company":"eGain Communications","jobTitle":"Product Management Analyst","website":""}}},"breadcrumbs":null,"channels":{"primary":{"name":"Technology","slug":"technology"},"sub":{"name":"Technology Solutions","slug":"technology-solutions"}},"meta":{"robots":"index, follow","description":"Learn how to design and build your own interactive menus inside of your PowerShell scripts to help non-PowerShell users more easily navigate and use them.","title":"Create A User Menu In A PowerShell Script - business.com"},"parsedContent":{"toc":[{"id":"how-to-build-an-interactive-menu","title":"How to build an interactive menu"},{"id":"final-step","title":"Final step"}],"json":[{"component":"p","children":["Learn how to design and build your own interactive menus inside of your PowerShell scripts to help non-PowerShell users more easily navigate and use them."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":["PowerShell scripts aren't generally written with interactivity in mind. Automation is all about hands-off activities, but there are times when you might need to write a script not for you but for others who might not know PowerShell like you do. You could write a GUI for your script but that can get complicated pretty quickly. Maybe you just need a way for a user to quickly provide some input to a script. To do this, you can build small interactive menus inside of a PowerShell script -- here's how."]}]},{"component":"h2","props":{"id":"how-to-build-an-interactive-menu"},"children":["How to build an interactive menu",{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/bbc7d51ee47d96a8b4595/1200_-900- 1200w, /images/content/5b6/bbc7d51ee47d96a8b4595/668_-501- 668w, /images/content/5b6/bbc7d51ee47d96a8b4595/320_-240- 320w","sizes":"668px, 100%"}}]}]},{"component":"p","children":["There's a few ways to create an interactive menu in PowerShell. One way is to use a .NET object ",{"component":"a","props":{"href":"https://technet.microsoft.com/en-us/library/Ff730939.aspx","target":"_blank"},"children":["System.Management.Automation.Host.ChoiceDescription"]}," and the PromptForChoice method. This is way is fine but I believe you have a little more control over the messaging if you design your own. This is what we'll be covering today."]},{"component":"p","children":["The menu that we'll be creating will be built using a single function called ",{"component":"strong","children":["Show-Menu"]}," and a ",{"component":"strong","children":["do/until loop"]},". We'll include a few options to choose from and an option to quit from the menu at any time. The premise is to present the user with a few options, allow them to run those option and then present the menu again if they'd like to run other options."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/bbcd551ee47d76a8b4585/742_-189- 742w, /images/content/5b6/bbcd551ee47d76a8b4585/412_-105- 412w, /images/content/5b6/bbcd551ee47d76a8b4585/206_-52- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/bbcd551ee47d76a8b4585/418-107-","alt":"","width":"418","height":"107","data-content-img-id":"5b6bbcd551ee47d76a8b4585"}}]}]},{"component":"p","children":["To get started, you need a way to build this menu in a PowerShell function. This needs to be in a function because whenever the user selects an option and the script runs whatever is in that option, we need to show the menu again."]},{"component":"p","children":["Here is a very simple ",{"component":"strong","children":["Show-Menu"]}," function. You'll notice that essentially it just does two things: Clears the host screen and then just writes some text to the screen. It needs to do nothing more. You could make this better by moving the options up as a parameter if you'd like, which would make it more reusable, but that's up to you."]},{"component":"blockquote","children":[{"component":"br"},"function Show-Menu",{"component":"br"}," {",{"component":"br"},"      param (",{"component":"br"},"            [string]$Title = 'My Menu'",{"component":"br"},"      )",{"component":"br"},"      cls",{"component":"br"},"      Write-Host \"================ $Title ================\"",{"component":"br"},"      ",{"component":"br"},"      Write-Host \"1: Press '1' for this option.\"",{"component":"br"},"      Write-Host \"2: Press '2' for this option.\"",{"component":"br"},"      Write-Host \"3: Press '3' for this option.\"",{"component":"br"},"      Write-Host \"Q: Press 'Q' to quit.\"",{"component":"br"}," }",{"component":"br"},{"component":"br"}]},{"component":"p","children":["Once the function is built, we'll then need a way to display it, provide user input and then, when whatever is going to happen actually happens when an option is chosen, we want to display the menu again. This is why we chose to use a do/until loop."]},{"component":"p","children":["A do/until loop executes code right off the bat. In this case, it shows the menu and then uses ",{"component":"strong","children":["Read-Host"]}," to prompt for a selection."]},{"component":"blockquote","children":[{"component":"br"},"do",{"component":"br"},"{",{"component":"br"},"      Show-Menu",{"component":"br"},"      $input = Read-Host \"Please make a selection\"",{"component":"br"},"      switch ($input)",{"component":"br"},"      {",{"component":"br"},"            '1' {",{"component":"br"},"                 cls",{"component":"br"},"                 'You chose option #1'",{"component":"br"},"            } '2' {",{"component":"br"},"                 cls",{"component":"br"},"                 'You chose option #2'",{"component":"br"},"            } '3' {",{"component":"br"},"                 cls",{"component":"br"},"                 'You chose option #3'",{"component":"br"},"            } 'q' {",{"component":"br"},"                 return",{"component":"br"},"            }",{"component":"br"},"      }",{"component":"br"},"      pause",{"component":"br"}," }",{"component":"br"}," until ($input -eq 'q')",{"component":"br"},{"component":"br"}]},{"component":"p","children":["We're then using a switch statement to make a decision on what code to execute based on the option selected. This is where you might run another script, reboot a server, set a registry key, delete a file, etc."]},{"component":"p","children":["Also, always be sure to include a \"Q\" option. This is a way for the user to break out of the loop if they're done with the menu. And notice the pause keyword; this is to prevent the menu from displaying extremely quickly again, but when it isn't necessarily required."]},{"component":"h2","props":{"id":"final-step"},"children":["Final step"]},{"component":"p","children":["Finally, the do loop will continually loop over and over again until the \"Q\" option. Once the \"Q\" option is selected, the until construct will be satisfied, which will then break out of the loop."]}]},"resource":null,"sponsorship":null,"thumbnail":{"path":"https://staging.business.com/images/content/5cf/a73019c658a06168b4568/1500-0-","caption":"Atstock Productions / Shutterstock","isBlurred":false},"bestPicks":null},"related":{"relatedArticles":[{"title":"PowerShell: Everything You Need to Know","canonical":"https://www.business.com/articles/powershell-everything-you-need-to-know/","slug":"powershell-everything-you-need-to-know","thumbnail":"https://www.business.com/images/content/5b8/998581039eb8f738b457b/1500-0-","dek":"What you need to know to unlock Microsoft PowerShell's full capabilities.","lastModified":"2018-09-05T17:00:00Z","published":"2018-09-05T17:00:00Z","sponsored":false},{"title":"From COVID-19 to Hurricane Season: Disaster Preparedness for Small Business","canonical":"https://www.business.com/articles/business-disaster-prep/","slug":"business-disaster-prep","thumbnail":"https://www.business.com/images/content/5e7/a495e5a215eb11d8b4569/1500-0-","dek":"Natural disasters – whether a hurricane, tornado or outbreak like COVID-19 – can have a disastrous effect on small businesses, but they don't have to. Here's how to weather a disaster successfully.","lastModified":"2020-03-25T15:19:00Z","published":"2018-09-26T07:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Managing Active Directory","canonical":"https://www.business.com/articles/powershell-active-directory-cmdlets/","slug":"powershell-active-directory-cmdlets","thumbnail":"https://www.business.com/images/content/5cf/90cab7b4374ab068b456f/1500-0-","dek":"Administering Active Directory is one of the most critical roles in any Windows network.","lastModified":"2018-09-04T08:00:00Z","published":"2018-09-04T08:00:00Z","sponsored":false},{"title":"Microsoft Azure vs. Amazon Web Services: Cloud Comparison","canonical":"https://www.business.com/articles/azure-vs-aws-cloud-comparison/","slug":"azure-vs-aws-cloud-comparison","thumbnail":"https://www.business.com/images/content/5cf/ff3445a215ed86a8b459d/1500-0-","dek":"Which one is right for your business?","lastModified":"2018-05-25T07:00:00Z","published":"2018-05-25T07:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Auditing And Maintaining Storage","canonical":"https://www.business.com/articles/powershell-for-storage/","slug":"powershell-for-storage","thumbnail":"https://www.business.com/images/content/5d0/18e065a215ea2498b4588/1500-0-","dek":"PowerShell v5 offers some new features for managing storage.","lastModified":"2018-09-04T11:00:00Z","published":"2018-09-04T11:00:00Z","sponsored":false},{"title":"Using Lean Strategies to Achieve Team Synergy and Efficiency","canonical":"https://www.business.com/articles/lean-strategies-team-efficiency/","slug":"lean-strategies-team-efficiency","thumbnail":"https://www.business.com/images/content/5be/480e49c658a51108b458c/1500-0-","dek":"Learn more about lean manufacturing strategies and how to implement them in your business.","lastModified":"2018-11-08T08:00:00Z","published":"2018-11-08T08:00:00Z","sponsored":false},{"title":"Understanding Functions in PowerShell","canonical":"https://www.business.com/articles/functions-in-powershell/","slug":"functions-in-powershell","thumbnail":"https://www.business.com/images/content/5d0/272227b4374087d8b4568/1500-0-","dek":"Instead of copying and pasting the same code over and over again, create a PowerShell func","lastModified":"2018-09-04T09:00:00Z","published":"2018-09-04T09:00:00Z","sponsored":false},{"title":"Your Guide to Creating a Small Business Marketing Plan","canonical":"https://www.business.com/articles/sample-marketing-plan-outline-and-template/","slug":"sample-marketing-plan-outline-and-template","thumbnail":"https://www.business.com/images/content/5f3/3e1765a215eb62d8b4567/1500-0-","dek":"Follow these templates and guidelines to get started on creating your business's marketing plan.","lastModified":"2020-08-12T12:28:00Z","published":"2018-03-02T13:00:00Z","sponsored":false},{"title":"How to Hire for Your Business","canonical":"https://www.business.com/articles/how-to-staff-business/","slug":"how-to-staff-business","thumbnail":"https://www.business.com/images/content/5ea/9c8bb9c658aee018b456c/1500-0-","dek":"Sterling Staffing Solutions CEO Stephen Carter shares his tips on staffing your business.","lastModified":"2020-07-31T15:00:00Z","published":"2018-08-16T12:00:00Z","sponsored":false},{"title":"Resources for Veteran-Owned Businesses","canonical":"https://www.business.com/articles/resources-veteran-owned-business/","slug":"resources-veteran-owned-business","thumbnail":"https://www.business.com/images/content/5b7/425da51ee47e7568b458a/1500-0-","dek":"How to start a business, find funding and gain customers as a veteran business owner.","lastModified":"2018-08-15T12:01:00Z","published":"2018-08-15T12:01:00Z","sponsored":false},{"title":"How and When to Create and Use PowerShell Modules","canonical":"https://www.business.com/articles/powershell-modules/","slug":"powershell-modules","thumbnail":"https://www.business.com/images/content/5cf/a71415a215e501f8b457a/1500-0-","dek":"Step-by-step instructions on creating and using modules.","lastModified":"2018-09-05T08:00:00Z","published":"2018-09-05T08:00:00Z","sponsored":false},{"title":"How to Make Money in Retirement: A Guide to Turning a Hobby into a Side Business","canonical":"https://www.business.com/articles/how-make-money-retirement-hobby/","slug":"how-make-money-retirement-hobby","thumbnail":"https://www.business.com/images/content/5ba/902e01039eb26648b4572/1500-0-","dek":"Many retirees turn side interests into income. Learn how to make money in retirement.","lastModified":"2018-09-24T15:03:00Z","published":"2018-09-24T15:03:00Z","sponsored":false}],"relatedReviews":[{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df908cc5a215e8c4a8b4568/conferencecall_Antonio_Diaz_Getty.jpg","canonical":"https://staging.business.com/categories/best-conference-call-services/","title":"The Best Conference Call Services of 2021","dek":"Which conference call service is right for your business in 2021? We review the top-rated services.","slug":"best-conference-call-services","lastModified":"2021-03-17T18:17:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df3d8695a215e58428b4570/POS_GETTY_DERO2084.jpg","canonical":"https://staging.business.com/categories/best-pos-systems/","title":"The Best POS Systems of 2021","dek":"Before you choose a POS system, check out our reviews of the best picks for 2021. ","slug":"best-pos-systems","lastModified":"2021-03-17T17:33:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5e00d4787b43745d6a8b456e/Laptop_undrey_getty.jpg","canonical":"https://staging.business.com/categories/employee-monitoring-software/","title":"The Best Employee Monitoring Software of 2021","dek":"We've narrowed down the best employee monitoring software for businesses. Check out our best picks and reviews.","slug":"employee-monitoring-software","lastModified":"2021-03-05T15:40:00Z"}],"relatedSponsoredArticles":null}},"pageContext":{"displayModified":"2018-09-05T08:00:00.000Z","template":"v1","title":"How To Build An Interactive Menu Inside Of A PowerShell Script","meta":{"robots":"index, follow"},"docType":"articles","slug":"powershell-interactive-menu","canonical":"https://staging.business.com/articles/powershell-interactive-menu/","skipTOC":false}},"staticQueryHashes":[]}