none
PowerShell Multiple Choice Menu

    Question

  • Building further on menu script that Bill_Stewart posted @ below link; Taking that as example, I want to tweak it so that I can select 'A' AND 'C' (more than one) choices and script should run BOTH functions/commands, either one after other or simultaneously. Is there a way?


    **************************
    do {
      do {
        write-host ""
        write-host "A - Selection A"
        write-host "B - Selection B"
        write-host "C - Selection C"
        write-host "D - Selection C"
        write-host ""
        write-host "X - Exit"
        write-host ""
        write-host -nonewline "Type your choice and press Enter: "
        $choice = read-host
        write-host ""
        $ok = @("A","B","C","D","X") -contains $choice
        if ( -not $ok) { write-host "Invalid selection" }
      }
      until ( $ok )
      switch ( $choice ) {
        "A" {
          write-host "You entered 'A'"
          break
        }
        "B" {
          write-host "You entered 'B'"
          break
        }
        "C" {
          write-host "You entered 'C'"
          break
        }
        "D" {
          write-host "You entered 'D'"
          break
        }
      }
    }
    until ( $choice -eq "X" )
    **************************

    Source: http://social.technet.microsoft.com/Forums/scriptcenter/en-US/737af85d-2681-450f-95cb-8afbd905684b/newbie-to-powershell-need-help-creating-a-menu

    Wednesday, December 18, 2013 3:56 PM

Answers

  • Sure, you can do that.  Here's one way of accomplishing it:

    do {
        do {
            write-host ""
            write-host "A - Selection A"
            write-host "B - Selection B"
            write-host "C - Selection C"
            write-host "D - Selection D"
            write-host ""
            write-host "X - Exit"
            write-host ""
            write-host -nonewline "Type your choice and press Enter: "
            
            $choice = read-host
            
            write-host ""
            
            $ok = $choice -match '^[abcdx]+$'
            
            if ( -not $ok) { write-host "Invalid selection" }
        } until ( $ok )
        
        switch -Regex ( $choice ) {
            "A"
            {
                write-host "You entered 'A'"
            }
            
            "B"
            {
                write-host "You entered 'B'"
            }
    
            "C"
            {
                write-host "You entered 'C'"
            }
    
            "D"
            {
                write-host "You entered 'D'"
            }
        }
    } until ( $choice -match "X" )
    

    I replaced all of the -eq and -contains operators with regular expressions, to deal with multi-character strings.  The switch statement has the -regex parameter added, and I've removed the Break statements from each block in the switch statement (so it will continue to check for other characters after finding one match.)
    • Marked as answer by Scriptacle Wednesday, December 18, 2013 5:20 PM
    Wednesday, December 18, 2013 4:36 PM

All replies

  • Sure, you can do that.  Here's one way of accomplishing it:

    do {
        do {
            write-host ""
            write-host "A - Selection A"
            write-host "B - Selection B"
            write-host "C - Selection C"
            write-host "D - Selection D"
            write-host ""
            write-host "X - Exit"
            write-host ""
            write-host -nonewline "Type your choice and press Enter: "
            
            $choice = read-host
            
            write-host ""
            
            $ok = $choice -match '^[abcdx]+$'
            
            if ( -not $ok) { write-host "Invalid selection" }
        } until ( $ok )
        
        switch -Regex ( $choice ) {
            "A"
            {
                write-host "You entered 'A'"
            }
            
            "B"
            {
                write-host "You entered 'B'"
            }
    
            "C"
            {
                write-host "You entered 'C'"
            }
    
            "D"
            {
                write-host "You entered 'D'"
            }
        }
    } until ( $choice -match "X" )
    

    I replaced all of the -eq and -contains operators with regular expressions, to deal with multi-character strings.  The switch statement has the -regex parameter added, and I've removed the Break statements from each block in the switch statement (so it will continue to check for other characters after finding one match.)
    • Marked as answer by Scriptacle Wednesday, December 18, 2013 5:20 PM
    Wednesday, December 18, 2013 4:36 PM
  • Thanks David! That works just fine and exactly what I wanted! Thanks a bunch :)

    Anyway I could add commas to choices? I currently have to add 'abcd' - can I do 'a,b,c,d' instead? I edited this line:

    $ok = $choice -match '^[a,b,c,d,x]+$'

    and now with or without commas works fine! Much Appreciated.....

    Wednesday, December 18, 2013 5:20 PM
  • The commas are unnecessary. What's provoking the question?

    Bill

    Wednesday, December 18, 2013 5:25 PM
    Moderator
  • Thanks Bill. I am going to have 20 or so choices; 1 to 20 or A to Z so wanted input choice to be least confusing and as neat as possible, but this works too and thanks for your original script :)
    Thursday, December 19, 2013 1:01 AM