Link to home
Start Free TrialLog in
Avatar of sabnews
sabnews

asked on

Deleting specific local printers via computer gpo script

Hi
I'm having trouble getting a local printer deleting script to work on WinXP machines. I can get the script to delete the computers local Printers when it boots but i want it to be more precise with what it actually deletes.
Our Prepress department have Creative Suite 2 which installs a local Adobe PDF printer. If I roll this script out as is it will delete this printer as well as the old ones we want to get rid of. I can not get it working as i keep getting an "unexpected next" error.
I want the script to be as generic as possible as there are loads of other departments with different print names so I would rather not have to specify each printer to delete.
Here is the code I have so far.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")

For Each objPrinter in colInstalledPrinters
If objPrinter.Name = "Adobe PDF" or "Microsoft Office Document Image Writer" then
next
else
objPrinter.Delete_
end if
Next

Avatar of and235100
and235100
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of Ron Malmstead
It's easier to do this in a batch file.  Instead of writing your own vbs, you should use the one that is built-in to windows.  See below.

cscript "%systemroot%\system32\prnmngr.vbs" -d -p "Microsoft Office Document Image Writer"
cscript "%systemroot%\system32\prnmngr.vbs" -d -p "Adobe PDF Writer"




Avatar of sabnews
sabnews

ASKER

Xuserx2000 I need something that will delete all local printers apart from the two mentioned above. That code would only delete those two printers.

and235100 there doesn't seem to be a solution on that page. The last part about using the registry to delete the printers would mean i would have to have 20 or so different scripts to cover all the departments and offices. I would like something that will delete all local printers apart from the ones I specify.

I just need a way for the loop to skip the printers i specify in the IF statement.
Avatar of sabnews

ASKER

I've got a little further. I have changed the code to an If not statement which now comes up with a type Mismatch error on the second printer in the IF NOT statement.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")

For Each objPrinter in colInstalledPrinters
If not objPrinter.Name = "Microsoft Office Document Image Writer" or "Adobe PDF" then
objPrinter.Delete_
end if
next

If I remove the "or" and the second printer name out of the IF NOT statement then it works fine and deletes all the printers apart from the one specified.
Does anybody know what I need to do to get the second part of the IF NOT statement working?
Avatar of sabnews

ASKER

Solved it now. Here is the script I'm putting into the startup gpo for the computers.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")

For Each objPrinter in colInstalledPrinters
If objPrinter.Name = "Microsoft Office Document Image Writer" or objPrinter.Name = "Adobe PDF" then
strdelete = false
else
objPrinter.Delete_
end if
next

Seems to work perfectly
Glad that you got it worked out.

Ask a new, 0-point question here https://www.experts-exchange.com/Community_Support/General/ to request that this question be closed, and as you provided an answer yourself, you will be entitled to a points refund.
ASKER CERTIFIED SOLUTION
Avatar of Vee_Mod
Vee_Mod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why don't you use a script to delete all the prtinters, then deploy the other ones via Group Policy