Geek Noise
Rants, rambles, news and notes by Peter Provost
21

Remove Unused Printer Ports

Friday, 21 May 2004 03:44 by Peter Provost

When I'm travelling around visiting clients, I often end up connecting to one of their local printers by creating my own TCP/IP printer port. But after a while, you end up with all of these damn ports and no way to get rid of them (that I could find).

Google to the rescue! I found this page on TechNet that had the following script on it. Works like a charm!


Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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_    'Yes that underscore matters!
    End If
Next

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   Technology
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed