#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”)
$Form.Add_KeyDown({if ($_.KeyCode -eq “Escape”)

#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

#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

#Add the button to our form

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

One thought on “New-Listbox-wMultiSelect

Have a code issue? Share your code by going to and pasting your code there, then post the link here!

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.