Written
December 16, 2010
if you are getting Access Denied errors when trying to run this script then add the following line after the "Set objWMIService = GetObject........." line
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
i.e.
************************************************************
Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
Set colPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colPrinters
objDictionary.Add objPrinter.PortName, objPrinter.PortName
Next
Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort")
For Each objPort in colPorts
If objDictionary.Exists(objPort.Name) Then
Else
ObjPort.Delete_
End If
Next
************************************************************
I'd like to credit where I found this after a lot of searching:
http://www.petri.co.il/forums/showthread.php?t=50755