kor Posted March 28, 2011 Share Posted March 28, 2011 I'm trying to find a way to simply delete a printer (any printer) inside a users profile based on the name of the printer. I don't want any user interaction, I just want the script to run, and poof, the printers in question are deleted. My searching is failing me. I don't want to delete the drivers for the printers as the users might want to add them back at a later date. I just want to get rid of them as choices in the printers and faxes window. (XP SP3 btw) Link to comment Share on other sites More sharing options...
MrMitchell Posted March 28, 2011 Share Posted March 28, 2011 (edited) Try post.Edit: Actually that might only be for network printers, dunno. If doesn't work, search for WMI delete printer and you should come up with some good hits.Edit2: Got the following (in almost its simplest form) to delete a printer off my machine:$strComputer = "." $strPrinter = "Brother HJ-400" $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $objPrinter = $objWMIService.Get("Win32_Printer.DeviceID='" & $strPrinter & "'") $objPrinter.Delete_ Edited March 28, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
kor Posted March 28, 2011 Author Share Posted March 28, 2011 Is there a way to check to see if the printer exists? I get an error when attempting to delete a printer that doesn't exist. This needs to be for a login script, so if the printer doesn't exist, it can skip trying to delete it. Link to comment Share on other sites More sharing options...
MrMitchell Posted March 28, 2011 Share Posted March 28, 2011 You can try this way instead (this error handling function is straight out of the help file, in the Obj/COM Reference): Global $g_eventerror = 0 $strComputer = "." $strPrinter = "Brother HJ-400" $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $collPrinters = $objWMIService.ExecQuery("Select * FROM Win32_Printer") For $printer In $collPrinters $name = $printer.name ConsoleWrite("Printer: " & $name & @CRLF) If StringInStr($name, $strPrinter) Then $printer.Delete_ If $g_eventerror Then ConsoleWrite("Error deleting printer: " & $name & @CRLF) Else ConsoleWrite("Deleted printer: " & $name & @CRLF) EndIf EndIf Next Func MyErrFunc() $HexNumber=hex($oMyError.number,8) ConsoleWrite("We intercepted a COM Error !" & @CRLF & _ "Number is: " & $HexNumber & @CRLF & _ "Windescription is: " & $oMyError.description & _ "Source is: " & $oMyError.source & @CRLF) $g_eventerror = 1 ; something to check for when this function returns EndFunc Instead of getting a specific printer by name this should just go thru all the printers and if the name contains $strPrinter then delete it. You could also use StringRegEx to match a printer name, or StringCompare if you want. If the .Delete_ function fails then it will throw the error and you can have the MyErrFunc() function do whatever you want. I'm not sure if you'll get an error if the PC doesn't have ANY printers, but most XP machines come with a few default printers anyway so I'm thinking that won't be a huge deal? Link to comment Share on other sites More sharing options...
kor Posted March 28, 2011 Author Share Posted March 28, 2011 I'll try it tomorrow and let you know. No, it shouldn't be a big deal. We are leaving a PDF Creator printer as an available option always, so there will always be at least 1 printer present. Link to comment Share on other sites More sharing options...
kor Posted March 29, 2011 Author Share Posted March 29, 2011 your code worked great. thank you. Link to comment Share on other sites More sharing options...
MrMitchell Posted March 29, 2011 Share Posted March 29, 2011 No problem, glad to hear it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now