Delete printers from windows 2000 programatically

staszko

Member
Joined
Jul 8, 2004
Messages
5
Programming Experience
3-5
Hello all, I'm not having any luck finding a way to delete a printer from windows 2000 from .net code or the command prompt. I don't know if it makes any difference, but the printers that I am looking to delete are automatically created by Remote Desktop when users sign on. Windows does not always delete them so I want to try to clean them up automatically myself.
I've tried the Win32_Printer WMI class, but it says that it does not support being deleted (I think that it might in XP and 2003, but I'm using 2000.)
I've also been trying rundll32 printui.dll,PrintUIEntry. Lots of sites list examples of how to create and delete printers, but the delete part does not work as they have documented it.
I looked at the net command in dos, but the printers I'm looking to delete do not show up in there.

Any help would be greatly appreciated. Thanks!
 

Mykre

Active member
Joined
Jun 14, 2004
Messages
28
Location
Australia
Programming Experience
3-5
Here is an example that will work on Windows 2003 and XP Print servers in vbscript, should be able to convert the syntax to VB.net
VB.NET:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters 
objPrinter.Delete_
Next
And here is an example to delete just one.
VB.NET:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer Where DriverName = 'HP QuietJet'")
For Each objPrinter in colInstalledPrinters 
objPrinter.Delete_
Next
Mykre
 
Last edited:

staszko

Member
Joined
Jul 8, 2004
Messages
5
Programming Experience
3-5
Do you have anything that will work with Windows 2000? That is the environment that I am trying to get this to work in.

Thanks
 

staszko

Member
Joined
Jul 8, 2004
Messages
5
Programming Experience
3-5
Solved!

In case anyone is interested, Microsoft has a dll available, prnadmin.dll, that can handle deleting printers in Windows 2000.
This solved my problem.
 
Top Bottom