Scrip - Form to install printers

Hello Everybody

So the rest have eluded me, I need some way to read the contents of the selected drop down list, so I can use it in the install button, have search the web, and I have gotten more confused, so know I try you Gurus :smiley:

Add-Type -AssemblyName system.windows.forms

$formobject=[System.Windows.forms.form]
$LabelObject=[System.Windows.Forms.Label]
$ComboBoxObject=[System.Windows.Forms.ComboBox]

#Set up Default Font
$DefaultFont='Verdana,10'


#Set up base form
$AppForm=New-Object $FormObject
$AppForm.ClientSize='500,300'
$AppForm.Text='Printer Install'
$AppForm.BackColor='#ffffff'
$AppForm.Font=$DefaultFont

#Building form
$lblPrinters=New-Object $LabelObject
$lblPrinters.Text='PrinterName :'
$lblPrinters.AutoSize=$true
$lblPrinters.Location=New-Object System.Drawing.Point(20,20)

$ddlPrinters=New-Object $ComboBoxObject
$ddlPrinters.Width='300'
$ddlPrinters.Location=New-Object System.Drawing.Point(140,20)

#load Drop down list
Get-Printer -ComputerName Printserver | ForEach-Object {$ddlPrinters.Items.Add($_.Name)}

$ddlPrinters.Text='Select Printer & Press install'


# Create install button
$Button = New-Object System.Windows.Forms.Button

$Button.Location = New-Object System.Drawing.Size(140,50)

$Button.Size = New-Object System.Drawing.Size(300,50)

$Button.Text = "Install"




#Execute button script

$Button.Add_Click(

{
Add-Printer \\printserver\$printqueue


}
)



#Add to form
$AppForm.Controls.AddRange(@($lblPrinters,$ddlPrinters,$Button))

#Show Form
$AppForm.ShowDialog()

##Garbage
$AppForm.Dispose()

Hi, welcome back :wave:

Use the SelectedItem property.

#Execute button script

$Button.Add_Click ( {
    $printQueue = $ddlPrinters.SelectedItem
    Add-Printer \\printserver\$printQueue
} )

Hi Matt

great, it works, now I just need to find out why the script stalls after printer installation :smiley: is there a way where I can have the button close the form too

Sure, you just add the close method:

$Button.Add_Click ( {
    $printQueue = $ddlPrinters.SelectedItem
    Add-Printer \\printserver\$printQueue
    $AppForm.Close()
} )