New-Listbox-wMultiSelect

#Load System Forms and Drawing assemblies, LoadWithPartialNames allows us to Tab Complete
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

#Users contains all users with a name like Fox-
$users = get-aduser -filter {Name -like “Fox-*”}

#begin to draw forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = “Pick a User”
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.StartPosition = “CenterScreen”

#Listen for Enter or Escape Button presses
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
{$x=$ListBox.SelectedItem;$Form.Close()}})
$Form.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
{$Form.Close()}})

#begin to draw list box
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = New-Object System.Drawing.Size(10,40)
$ListBox.Size = New-Object System.Drawing.Size(160,20)
$ListBox.Height = 80
$ListBox.Name = ‘ListBox_UserName’
$ListBox.SelectionMode = “MultiExtended”

#For each object within Users…
$users| ForEach-Object{
#the [void] below adds this element to our form, but suppresses any output associated
[void] $ListBox.Items.Add($_.Name)
}

#Add the combo box to the form
$Form.Controls.Add($ListBox)

#Set Properties for the Ok button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(180,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”

#What to do when the button is clicked, you can treat this like a function
$OKButton.Add_Click({$Form.Close()})

#Add the button to our form
$Form.Controls.Add($OKButton)

#Activate the form and display
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
Write-host “Selected items : ” $Listbox.SelectedItems

Advertisements

One thought on “New-Listbox-wMultiSelect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s