none
Convert a command line menu to a GUI menu RRS feed

  • Question

  • I have this menu that works great, as it is supposed to, but I would like to convert it to a GUI menu; had no luck with Out-Gridview.... Any ideas that preserves the functionality?

    Do {
    Write-Host "
    ----------Migration Menu----------
    1 = Printer Migration
    2 = User Profile Migration
    ----------------------------------"
    $choice1 = read-host -prompt "Select number & press enter"
    } until ($choice1 -eq "1" -or $choice1 -eq "2")

    Switch ($choice1) {
    "1" {Invoke-Expression -Command ($USB + "\Scripts\PrinterMigration.bat")}
    "2" {Invoke-Expression -Command ($USB + "\Scripts\UserMigrationMenu.bat")}
    }


    Jason Ostrowski

    Wednesday, October 10, 2018 6:57 PM

Answers

  • As Bill notes there is no way to convert PowerShell menus to a GUI menu.

    You can do this:

    function Show-Demo_Prompt_Menu {
        param(
            [hashtable]$Prompts
        )
    
    	Add-Type -AssemblyName System.Windows.Forms
        
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$flowlayoutpanel1 = New-Object 'System.Windows.Forms.FlowLayoutPanel'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'	
    	
    	$form1_Load={
    		$Prompts.GetEnumerator() |
    	        ForEach-Object{
    	            $rb = New-Object System.Windows.Forms.RadioButton
    	            $rb.Name = $_.Key
    	            $rb.Text = $_.Value
    	            $flowlayoutpanel1.Controls.Add($rb)
    	            $rb.add_CheckedChanged({
                             $script:result = $this.Name
                             $form1.Close()
                        })
    	        }
    	}
    	
    	$Form_StateCorrection_Load =	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$form1.SuspendLayout()
    	#
    	# form1
    	#
    	$form1.Controls.Add($flowlayoutpanel1)
    	$form1.AutoScaleDimensions = '8, 17'
    	$form1.AutoScaleMode = 'Font'
    	$form1.ClientSize = '376, 331'
    	$form1.Name = 'form1'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# flowlayoutpanel1
    	#
    	$flowlayoutpanel1.Dock = 'Fill'
    	$flowlayoutpanel1.Location = '0, 0'
    	$flowlayoutpanel1.Margin = '4, 4, 4, 4'
    	$flowlayoutpanel1.Name = 'flowlayoutpanel1'
    	$flowlayoutpanel1.Size = '376, 331'
    	$flowlayoutpanel1.TabIndex = 0
        $flowlayoutpanel1.FlowDirection = 'TopDown'
    	$form1.ResumeLayout()
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	[void]$form1.ShowDialog()
            $result
    }
    
    #Call the form
    $prompts = @{
        Test1 = 'prompt for test1'
        Test2 = 'prompt for test2'
        Test3 = 'prompt for test3'
        Test4 = 'prompt for test4'
        Test5 = 'prompt for test5'
    }
    Show-Demo_Prompt_Menu -prompts $prompts

    Look in the Gallery for more methods to create GUI menus.


    \_(ツ)_/




    Thursday, October 11, 2018 2:28 AM
  • You you use Out-Gridview with the -Passthru parameter to list your options so they can select one

    I rewrote your do-untilla little bit:

    Do {
    Write-Host "
    ----------Migration Menu----------
    1 = Printer Migration
    2 = User Profile Migration
    ----------------------------------"
    
    $Choice = "Printer Migration","User Profile Migration"
    $Selection = $Choice | Out-GridView -Title "Migration Menu" -PassThru
    
    } until ($Selection -ne $null)
    
    Switch ($Selection) {
    "Printer Migration" {Write-Output "Choice 1"}
    "User Profile Migration" {Write-Output "Choice 2"}
    }



    Thursday, October 11, 2018 7:37 AM

All replies

  • The short answer to your question is no.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 10, 2018 7:11 PM
    Moderator
  • As Bill notes there is no way to convert PowerShell menus to a GUI menu.

    You can do this:

    function Show-Demo_Prompt_Menu {
        param(
            [hashtable]$Prompts
        )
    
    	Add-Type -AssemblyName System.Windows.Forms
        
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$flowlayoutpanel1 = New-Object 'System.Windows.Forms.FlowLayoutPanel'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'	
    	
    	$form1_Load={
    		$Prompts.GetEnumerator() |
    	        ForEach-Object{
    	            $rb = New-Object System.Windows.Forms.RadioButton
    	            $rb.Name = $_.Key
    	            $rb.Text = $_.Value
    	            $flowlayoutpanel1.Controls.Add($rb)
    	            $rb.add_CheckedChanged({
                             $script:result = $this.Name
                             $form1.Close()
                        })
    	        }
    	}
    	
    	$Form_StateCorrection_Load =	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$form1.SuspendLayout()
    	#
    	# form1
    	#
    	$form1.Controls.Add($flowlayoutpanel1)
    	$form1.AutoScaleDimensions = '8, 17'
    	$form1.AutoScaleMode = 'Font'
    	$form1.ClientSize = '376, 331'
    	$form1.Name = 'form1'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# flowlayoutpanel1
    	#
    	$flowlayoutpanel1.Dock = 'Fill'
    	$flowlayoutpanel1.Location = '0, 0'
    	$flowlayoutpanel1.Margin = '4, 4, 4, 4'
    	$flowlayoutpanel1.Name = 'flowlayoutpanel1'
    	$flowlayoutpanel1.Size = '376, 331'
    	$flowlayoutpanel1.TabIndex = 0
        $flowlayoutpanel1.FlowDirection = 'TopDown'
    	$form1.ResumeLayout()
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	[void]$form1.ShowDialog()
            $result
    }
    
    #Call the form
    $prompts = @{
        Test1 = 'prompt for test1'
        Test2 = 'prompt for test2'
        Test3 = 'prompt for test3'
        Test4 = 'prompt for test4'
        Test5 = 'prompt for test5'
    }
    Show-Demo_Prompt_Menu -prompts $prompts

    Look in the Gallery for more methods to create GUI menus.


    \_(ツ)_/




    Thursday, October 11, 2018 2:28 AM
  • You you use Out-Gridview with the -Passthru parameter to list your options so they can select one

    I rewrote your do-untilla little bit:

    Do {
    Write-Host "
    ----------Migration Menu----------
    1 = Printer Migration
    2 = User Profile Migration
    ----------------------------------"
    
    $Choice = "Printer Migration","User Profile Migration"
    $Selection = $Choice | Out-GridView -Title "Migration Menu" -PassThru
    
    } until ($Selection -ne $null)
    
    Switch ($Selection) {
    "Printer Migration" {Write-Output "Choice 1"}
    "User Profile Migration" {Write-Output "Choice 2"}
    }



    Thursday, October 11, 2018 7:37 AM
  • Both options work! I can't wait until my scripting skills get up to this type of level; you guys always are a great help! One question for JRV on your script, the order seems to be random and I'm in the process of researching how to have the options displayed in the order of how they are listed... Is there a quick fix? As well as change the title from foms to something else

    Jason Ostrowski


    Thursday, October 11, 2018 1:19 PM
  • Hi Toosje

    I had to make a slight change to your script, instead of:

    until ($Selection -ne $null)

    I changed it to: 

    until ($Selection -ne $null -or $Selection -eq $null)

    Because I couldn't exit out of the Out-GridView window unless I made a selection.... Now I can

    Thanks for your assistance!


    Jason Ostrowski

    Thursday, October 11, 2018 5:32 PM
  • To enforce an order add a number to the label and sort the by the values.  We can also create a collection of objects that will maintain an order.

    Start by learning PowerShell and the rest will be easy.

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    Thursday, October 11, 2018 5:34 PM
  • Yes that's true, but it was the same with your setup if they didn't pick a number 1 or 2 ;)

    But in that case you can just remove the do-untill, so its not a loop anymore in which they have to make a decision

    Thursday, October 11, 2018 8:51 PM