[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2009-04-01
5
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 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