Script - How Can I...
 
Bildirimler
Hepsini Temizle

Script - How Can I Remove All the Local Printers from a Computer?  

  RSS
Rahmi DILLI
(@rahmidilli)
Tecrübeli Üye

Hey, JW. You did say that you wanted to do this only on
computers running Windows XP or Windows Server 2003, didn’t you? You
didn’t? Oh. Well, we’re going to pretend that you did, because this is
a very easy task to carry out on Windows XP and Windows Server 2003.
That’s because on Windows XP and Windows Server 2003 the WMI class Win32_Printer not only can easily distinguish between local printers and network printers, but it also supports the Delete_
method. As you can probably guess, that method enables you to delete
printer connections. In other words, WMI lets you pick out and remove
local printers. Which, all in all, sounds exactly like the answer to
your question.

That’s the good news. The bad news is that, on
earlier versions of Windows, it’s more complicated to pick out local
printers; even worse, the Win32_Printer class doesn’t support the
Delete_ method. That means it’s far more challenging to identify and
remove local printers. Because of that we’re going to focus on removing
local printers from Windows XP and Windows Server 2003 computers and
hope that takes care of your problem. If not, we’ll have to tackle
printer removal on Windows 2000 computers at a later date.

For Windows XP and Windows Server 2003 users you can delete all the local printers using a script as simple as this:

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

This script begins by connecting to the WMI service on the
local computer (although it can be easily modified to remove printers
from a remote computer instead). We then use this line of code to
return a collection of all the local printers; as far as WMI is
concerned, a printer is a local printer if the Network property is set to False:

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

Once we have a collection of local printers we simply set up a
For Each loop to walk through that collection, calling the Delete_
method (note the underscore on the end) for each printer in the
collection:

For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next

There you have it: that will delete all the local printers, while leaving the network printer connections alone. Now
aren’t you glad that you asked about removing local printers
specifically from Windows XP and Windows Server 2003 computers without
giving any thought whatsoever to earlier versions of the operating
system?

OK, fine: we’ll see what we can do about addressing
this issue on Windows 2000 computers. We won’t make any promises, but
we’ll look into it.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec05/hey1201.mspx 

Alıntı
Gönderildi : 22/08/2008 14:35
Paylaş: