Solved

Remove all Local Printers Except for ......

Posted on 2009-04-01
5
235 Views
Last Modified: 2012-05-06
Here is my script it is tested to remove EVRYTHING LOCAL.

Can someone give me a line of code that would EXCLUDE a specific printers
....for example a printer that contains "PDF" or "XPS" or "Office"

'This removes all Local Printers

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

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

For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next

0
Comment
Question by:dirkdigs
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 24047218
seems like sth like this (for searching PDF in Caption value for Printer)


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 InStr(objPrinter.Caption, "PDF") = 0 Then

    objPrinter.Delete_

else

	' do nothing - the printer string found

End If

Next

Open in new window

0
 

Author Comment

by:dirkdigs
ID: 24051357
Wil this work if the word "PDF" is found Anywhere in the printers name?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 24052364
to be precise: it would work for the printers where the caption value for win32_printers contains PDF string. so yes.

as to be more precise:
http://msdn.microsoft.com/en-us/library/aa394363(VS.85).aspx

it should rather be name value (below snippet) so this is correction:

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 InStr(objPrinter.Name, "PDF") = 0 Then

    objPrinter.Delete_

else

        ' do nothing - the printer string found

End If

Next

Open in new window

0
 

Author Comment

by:dirkdigs
ID: 24054338
i would like to purge the print queue before deleting., where do i add that in just before objPrinter.Delete_
 ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 24054773
this is the code for cancelling all the jobs for "HP LaserJet 1018" name

download wmi code creator, migh be a handy one
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en


strComputer = "." 

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

' Obtain an instance of the the class 

' using a key property value.

Set objShare = objWMIService.Get("Win32_Printer.DeviceID='HP LaserJet 1018'")
 

' no InParameters to define
 

' Execute the method and obtain the return status.

' The OutParameters object in objOutParams

' is created by the provider.

Set objOutParams = objWMIService.ExecMethod("Win32_Printer.DeviceID='HP LaserJet 1018'", "CancelAllJobs")
 

' List OutParams

Wscript.Echo "Out Parameters: "

Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now