Create Dropdown and pass selection back to script

jat0369 used Ask the Experts™
I have a script that builds a CSV file (Name, Description).

I'd like to have some sort of pop-up occur that has a dropdown of only one of the collumns. Once the item is selected and the user clicks okay, it sends that variable back to the script for further processing.  I'm at a loss of how to go about doing this.   Would someone please help me out and show me a simple example of how to do this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I have written a function ( that takes a collection of objects, enable to use to select from either a text menu, list box or checked list box, either single or multiple selections, and the selected objects are output to the pipe for further processing.


The Select-ItemFromList utility receives a collection of objects and will
display the list as either a numbered text list as a menu or in a list
box for selection. There is a switch to enable multiple item selection.

-------------------------- EXAMPLE 1 --------------------------
C:\PS>Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode None
-------------------------- EXAMPLE 2 --------------------------
C:\PS>Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode MultipleSimple
-------------------------- EXAMPLE 3 --------------------------
C:\PS>Select-ItemFromList -option (Get-Process) -displayProperty ProcessName -mode ListBox -selectionMode MultipleExtended
-------------------------- EXAMPLE 4 --------------------------
C:\PS>Select-ItemFromList -option (Get-Process) -displayProperty ProcessName -mode CheckedListBox


This is exactly what I'm looking for. Great work!
I took the SelectItemFromList.txt and renamed it dropdown.ps1
I referenced the file and called the function and It's giving me a bit of a problem. What am I doing wrong?

PS C:\Install\POS_PREP> $thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
PS C:\Install\POS_PREP> . ($thisScript + '.\dropdown.ps1')
PS C:\Install\POS_PREP> Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode None
Unable to find type [System.Windows.Forms.SelectionMode]: make sure that the assembly containing this type is loaded.
At C:\Install\POS_PREP\dropdown.ps1:73 char:41
+     [System.Windows.Forms.SelectionMode] <<<< $selectionMode = [System.Windows.Forms.SelectionMode]::One
    + CategoryInfo          : InvalidOperation: (System.Windows.Forms.SelectionMode:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

PS C:\Install\POS_PREP>

Open in new window


Hi BChallis - any update to this? Any idea as to what I'm doing wrong?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a computer:"
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(260,20) 
$objListBox.Height = 80

 ForEach($i in (Get-Content Names.txt))
 {[void] $objListBox.Items.Add($i)}

$objForm.Topmost = $True

[void] $objForm.ShowDialog()

Open in new window


I ended up solving the issue myself using the code supplied. It's not as clean as the version bchallis supplied, but it works.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial