#requires -pssnapin pseventing # http://www.codeplex.com/pseventing (1.0 or 1.1) param([string[]]$choices = $(throw "please supply a string array of choices")) if ($choices.length -eq 0) { Write-Warning "cannot be a zero length array." return } # initialize form $form = new-object windows.forms.form $form.Text = "Choose..." $form.MinimizeBox = $false $form.MaximizeBox = $false $form.AutoSize = $true $form.AutoSizeMode = "GrowAndShrink" # initialize listbox $listbox = new-object windows.forms.listbox $choices | % { [void]$listbox.items.add($_) } $form.controls.Add($listbox) # catch a choice in the listbox (remove -verbose for quiet mode) Connect-EventListener -VariableName listbox -EventName SelectedIndexChanged -Verbose # catch someone closing the form (remove -verbose for quiet mode) Connect-EventListener -VariableName form -EventName Closed -Verbose $form.Show() # wait for an event while performing sendmessage pumping (or ctrl+c to exit) $event = Get-Event -Wait # don't pollute pipeline (remove in production) write-host ($event | ft -auto | out-string) $form.Dispose() $choice = $listbox.SelectedItem # clean up; event listeners will be automatically unhooked ;-) $form = $null $listbox = $null # return chosen item, or $null if the form was closed if ($event.Name -eq "SelectedIndexChanged") { $choice } else { $null # cancelled }