Solved

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

Posted on 2009-04-01
5
237 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 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