When creating forms and GUI through PowerShell you can use the .NET Class System.Windows.Forms. This example will describe how to make a basic dopdown form and return the selected value to PowerShell.
First we create an Array holding the DopDown values.PS > [array]$DropDownArray = "First Choice", "Second Choice", "Third Choice"The Array contains 3 choices. You can modify this to your own specifications or you can retrieve a list from an Internet Page, Active-Directory or whatever you want to display. Just make sure that its presented in an Array ( put [array] in front of the variable holding the Array to assure that the object will be an Array). Next we’ll create a function that defines what will happen after the Form Choice has been made.
PS > function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
Write-Host $Choice
}
The function Stores the Choice in a variable and converts it To a String Value, then it closes the form and finally returns the Choice made. Now all we need to do is create a Form.
Step one in creating a form is making PowerShell aware of the Windows.Forms .NET class. We do this by loading the Class into the Global Assembly Cache. We also load the System.Drawing Class since it’s useful when working with Forms.
PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
You can pipe it to Out-Null if you don’t want the returnvalue displayed.
Now we can create our form! First we create the body of the Form. There are alot of properties that are settable but we’ll keep it simple in this example.
PS > $Form = New-Object System.Windows.Forms.Form PS > $Form.width = 300 PS > $Form.height = 150 PS > $Form.Text = ”DropDown”Now that we have a body for our form we can create a Dropdown list. Here we will define Size and Location through System.Drawing. It lets us set the location based on X and Y coordinates where 0,0 is top left corner. The DropDown list is created through Windows.Forms.ComboBox.
PS > $DropDown = new-object System.Windows.Forms.ComboBox PS > $DropDown.Location = new-object System.Drawing.Size(100,10) PS > $DropDown.Size = new-object System.Drawing.Size(130,30)To add our Array to the DropDown List we have to loop through it and use the Add() method on each value in the Array.
PS > ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item)
}
Now we can add the DropDown List to our Form. This is done through the Controls.Add() method on the $Form object.
PS > $Form.Controls.Add($DropDown)Adding a Label to the DropDown menu is done through Windows.Forms.Label. By changing the Location property you can move the label around. The Text property sets the Text shown in the Label.
PS > $DropDownLabel = new-object System.Windows.Forms.Label PS > $DropDownLabel.Location = new-object System.Drawing.Size(10,10) PS > $DropDownLabel.size = new-object System.Drawing.Size(100,20) PS > $DropDownLabel.Text = "Items" PS > $Form.Controls.Add($DropDownLabel)Almost done, now all we need is a confirmation button that executes the function we made earlier. The button is made through Windows.Forms.Button and the the Add_Click() method is used to tell the button what to do when you click on it.
PS > $Button = new-object System.Windows.Forms.Button
PS > $Button.Location = new-object System.Drawing.Size(100,50)
PS > $Button.Size = new-object System.Drawing.Size(100,20)
PS > $Button.Text = "Select an Item"
PS > $Button.Add_Click({Return-DropDown})
PS > $form.Controls.Add($Button)
Now all we have to do is start the Form. First we activate it and then use the ShowDialog() method to display it on screen.
PS > $Form.Add_Shown({$Form.Activate()})
PS > $Form.ShowDialog()
And here is what the form looks like:
Below is the code used in this example
# Edit This item to change the DropDown Values
[array]$DropDownArray = "First Choice", "Second Choice", "Third Choice"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
Write-Host $Choice
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
$DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
[?]