GUI to delete a printer
hello, having trouble this. trying display list of printers , delete them using winforms. failing. appreciated.
[system.reflection.assembly]::loadwithpartialname("system.windows.forms") function remove-printer($printobject, [string]$printerpath) { $dp = $printobject | where-object { $_.deviceid -match $printerpath} $dp.psbase.delete() } $winform = new-object windows.forms.form $winform.text = "delete printer" $winform.size = new-object drawing.size(400,150) $deletebutton = new-object windows.forms.button $deletebutton.location = new-object drawing.size(220,10) $deletebutton.text = "delete" $winform.controls.add($deletebutton) $listbox = new-object windows.forms.listbox $listbox.location = new-object drawing.size(10,10) $listbox.size = new-object drawing.size(200,100) $winform.controls.add($listbox) $printers = (get-wmiobject -class win32_printer) $printersfile = $printers | ft name -hidetableheaders remove-item c:\_tmp\printers.txt $printersfile > c:\_tmp\printers.txt (get-content c:\_tmp\printers.txt) | ? {$_.trim() -ne "" } | set-content c:\_tmp\printers.txt foreach($i in (get-content c:\_tmp\printers.txt)) { $listbox.items.add($i) | out-null } $winform.add_shown($winform.activate()) $listbox.selecteditem $deletebutton.add_click( { (get-wmiobject -q "select * win32_printer name='$listbox.selecteditem'").psbase.delete() } ) $winform.showdialog()
add @ beginning of get-printer function
$listbox.items.clear()
clear out contents of listbox prior re-adding printers.
Windows Server > Windows PowerShell
Comments
Post a Comment