Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

21 Experts available now in Live!

Get 1:1 Help Now