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.
Locked
probles40
Posts: 15
Joined: Sat Apr 11, 2020 6:05 pm

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.  
  3. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  4.  
  5.  
  6.  
  7. # Array of "printer objects"
  8.  
  9. $Printers = Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Select-object Name, Default
  10.  
  11. foreach-object{@(
  12.  
  13.     New-Object psobject -Property @{
  14.  
  15.         Name = $_.Name
  16.  
  17.         SetCommand = { ((New-Object -ComObject WScript.Network).SetDefaultPrinter($_.Name)) }
  18.  
  19.     }    
  20.  
  21. )}
  22.  
  23.  
  24.  
  25. $objForm = New-Object System.Windows.Forms.Form
  26.  
  27. $objForm.Text = "Select a Printer"
  28.  
  29. $objForm.Size = New-Object System.Drawing.Size(400,200)
  30.  
  31. $objForm.StartPosition = "CenterScreen"
  32.  
  33.  
  34.  
  35. $objForm.KeyPreview = $True
  36.  
  37. $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
  38.  
  39.     {$x=$objListBox.SelectedItem;$objForm.Close()}})
  40.  
  41. $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
  42.  
  43.     {$objForm.Close()}})
  44.  
  45.  
  46.  
  47. #Ok Button
  48.  
  49. $OKButton = New-Object System.Windows.Forms.Button
  50.  
  51. $OKButton.Location = New-Object System.Drawing.Size(75,120)
  52.  
  53. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  54.  
  55. $OKButton.Text = "OK"
  56.  
  57. $OKButton.Add_Click({
  58.  
  59.     # Grab the printer array index
  60.  
  61.     $index = $objListBox.SelectedIndex
  62.  
  63.  
  64.  
  65.     # Execute the appropriate command
  66.  
  67.     $Printers[$index].SetCommand
  68.  
  69.  
  70.  
  71.     # Exit
  72.  
  73.     $objForm.Close()
  74.  
  75. })
  76.  
  77. $objForm.Controls.Add($OKButton)
  78.  
  79.  
  80.  
  81. #Cancel Button
  82.  
  83. $CancelButton = New-Object System.Windows.Forms.Button
  84.  
  85. $CancelButton.Location = New-Object System.Drawing.Size(150,120)
  86.  
  87. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  88.  
  89. $CancelButton.Text = "Cancel"
  90.  
  91. $CancelButton.Add_Click({$objForm.Close()})
  92.  
  93. $objForm.Controls.Add($CancelButton)
  94.  
  95.  
  96.  
  97. $objLabel = New-Object System.Windows.Forms.Label
  98.  
  99. $objLabel.Location = New-Object System.Drawing.Size(10,20)
  100.  
  101. $objLabel.Size = New-Object System.Drawing.Size(280,20)
  102.  
  103. $objLabel.Text = "Please select a printer:"
  104.  
  105. $objForm.Controls.Add($objLabel)
  106.  
  107.  
  108.  
  109. #List box showing printer options
  110.  
  111. $objListBox = New-Object System.Windows.Forms.ListBox
  112.  
  113. $objListBox.Location = New-Object System.Drawing.Size(10,40)
  114.  
  115. $objListBox.Size = New-Object System.Drawing.Size(360,20)
  116.  
  117. $objListBox.Height = 80
  118.  
  119.  
  120.  
  121. foreach($Printer in $Printers){
  122.  
  123.     [void] $objListBox.Items.Add($Printer.Name)
  124.  
  125. }
  126.  
  127.  
  128.  
  129. $objForm.Controls.Add($objListBox)
  130.  
  131.  
  132.  
  133. $objForm.Topmost = $True
  134.  
  135.  
  136.  
  137. $objForm.Add_Shown({$objForm.Activate()})
  138.  
  139. [void] $objForm.ShowDialog()

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Sat Apr 11, 2020 6:05 pm

Re: PowerShell: Using GUI for Selecting Default Printer

Post by probles40 »

this post can be closed.

Locked