PowerShell: Using GUI for Selecting Default Printer

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 3 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
probles40
Posts: 15
Last visit: Mon Apr 27, 2020 8:41 am

PowerShell: Using GUI for Selecting Default Printer

Post by probles40 »

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?
  1. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  2. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  3.  
  4. # Array of "printer objects"
  5. $Printers = Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Select-object Name, Default
  6. foreach-object{@(
  7.     New-Object psobject -Property @{
  8.         Name = $_.Name
  9.         SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter($_.Name)) }
  10.     }    
  11. )}
  12.  
  13. $objForm = New-Object System.Windows.Forms.Form
  14. $objForm.Text = "Select a Printer"
  15. $objForm.Size = New-Object System.Drawing.Size(400,200)
  16. $objForm.StartPosition = "CenterScreen"
  17.  
  18. $objForm.KeyPreview = $True
  19. $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
  20.     {$x=$objListBox.SelectedItem;$objForm.Close()}})
  21. $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
  22.     {$objForm.Close()}})
  23.  
  24. #Ok Button
  25. $OKButton = New-Object System.Windows.Forms.Button
  26. $OKButton.Location = New-Object System.Drawing.Size(75,120)
  27. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  28. $OKButton.Text = "OK"
  29. $OKButton.Add_Click({
  30.     # Grab the printer array index
  31.     $index = $objListBox.SelectedIndex
  32.  
  33.     # Execute the appropriate command
  34.     $Printers[$index].SetCommand
  35.  
  36.     # Exit
  37.     $objForm.Close()
  38. })
  39. $objForm.Controls.Add($OKButton)
  40.  
  41. #Cancel Button
  42. $CancelButton = New-Object System.Windows.Forms.Button
  43. $CancelButton.Location = New-Object System.Drawing.Size(150,120)
  44. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  45. $CancelButton.Text = "Cancel"
  46. $CancelButton.Add_Click({$objForm.Close()})
  47. $objForm.Controls.Add($CancelButton)
  48.  
  49. $objLabel = New-Object System.Windows.Forms.Label
  50. $objLabel.Location = New-Object System.Drawing.Size(10,20)
  51. $objLabel.Size = New-Object System.Drawing.Size(280,20)
  52. $objLabel.Text = "Please select a printer:"
  53. $objForm.Controls.Add($objLabel)
  54.  
  55. #List box showing printer options
  56. $objListBox = New-Object System.Windows.Forms.ListBox
  57. $objListBox.Location = New-Object System.Drawing.Size(10,40)
  58. $objListBox.Size = New-Object System.Drawing.Size(360,20)
  59. $objListBox.Height = 80
  60.  
  61. foreach($Printer in $Printers){
  62.     [void] $objListBox.Items.Add($Printer.Name)
  63. }
  64.  
  65. $objForm.Controls.Add($objListBox)
  66.  
  67. $objForm.Topmost = $True
  68.  
  69. $objForm.Add_Shown({$objForm.Activate()})
  70. [void] $objForm.ShowDialog()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell: Using GUI for Selecting Default Printer

Post by jvierra »

As always we cannot tell what line 34 is and we cannot see the error. Please fix this and post the complete error.
probles40
Posts: 15
Last visit: Mon Apr 27, 2020 8:41 am

Re: PowerShell: Using GUI for Selecting Default Printer

Post by probles40 »

this post can be closed.
This topic is 3 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked