- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- # Array of "printer objects"
- $Printers = Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Select-object Name, Default
- foreach-object{@(
- New-Object psobject -Property @{
- Name = $_.Name
- SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter($_.Name)) }
- }
- )}
- $objForm = New-Object System.Windows.Forms.Form
- $objForm.Text = "Select a Printer"
- $objForm.Size = New-Object System.Drawing.Size(400,200)
- $objForm.StartPosition = "CenterScreen"
- $objForm.KeyPreview = $True
- $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
- {$x=$objListBox.SelectedItem;$objForm.Close()}})
- $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
- {$objForm.Close()}})
- #Ok Button
- $OKButton = New-Object System.Windows.Forms.Button
- $OKButton.Location = New-Object System.Drawing.Size(75,120)
- $OKButton.Size = New-Object System.Drawing.Size(75,23)
- $OKButton.Text = "OK"
- $OKButton.Add_Click({
- # Grab the printer array index
- $index = $objListBox.SelectedIndex
- # Execute the appropriate command
- $Printers[$index].SetCommand
- # Exit
- $objForm.Close()
- })
- $objForm.Controls.Add($OKButton)
- #Cancel Button
- $CancelButton = New-Object System.Windows.Forms.Button
- $CancelButton.Location = New-Object System.Drawing.Size(150,120)
- $CancelButton.Size = New-Object System.Drawing.Size(75,23)
- $CancelButton.Text = "Cancel"
- $CancelButton.Add_Click({$objForm.Close()})
- $objForm.Controls.Add($CancelButton)
- $objLabel = New-Object System.Windows.Forms.Label
- $objLabel.Location = New-Object System.Drawing.Size(10,20)
- $objLabel.Size = New-Object System.Drawing.Size(280,20)
- $objLabel.Text = "Please select a printer:"
- $objForm.Controls.Add($objLabel)
- #List box showing printer options
- $objListBox = New-Object System.Windows.Forms.ListBox
- $objListBox.Location = New-Object System.Drawing.Size(10,40)
- $objListBox.Size = New-Object System.Drawing.Size(360,20)
- $objListBox.Height = 80
- foreach($Printer in $Printers){
- [void] $objListBox.Items.Add($Printer.Name)
- }
- $objForm.Controls.Add($objListBox)
- $objForm.Topmost = $True
- $objForm.Add_Shown({$objForm.Activate()})
- [void] $objForm.ShowDialog()
PowerShell: Using GUI for Selecting Default Printer
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
PowerShell: Using GUI for Selecting Default Printer
I have a code that can query existing printers on the end-users computer. It allows to make a single selection for the default printer. However, it is not working and gives an error, in line 34. Can someone please take a look and critic what is wrong?