Question

Script to delete local printer port

Asked by: BalchBingham

I need a vb script to delete a local printer port.  Through Googling I've found lots of ways to delete TCP/IP Ports, but nothing about deleting a local port.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-14 at 17:06:31ID24322615
Tags

Windows XP

,

Printers

,

Local Printer Port

Topics

VB Script

,

Windows XP Operating System

,

Printers

Participating Experts
4
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Adding TCP/IP printers with batch
    Win2000 and XP workstations need new printers installed Everyones printer and faxes window is cluttered with old unused printers that need to be removed. Original configuration was a TCP/IP port directly (to the printer with static IP address) from each workstation. Then I ...
  2. Add Standard TCP/IP Printer
    I would like to add networked printers using a Standard TCP/IP Printer port remotly to group of Workstations on my network. This printer should be available to any users that log onto the WS. Is there a script or GPO that could do this? I would like to this so not to use a ...
  3. Auto mapping local ip printers with VBS
    I need to wrote a VB script that will create a standard tcp/ip port and map a printer to it. Can this be done? I have an office with 9 printer stations, each printer station has about 3 printers. Some of the printers require special print queue settings. I would like to c...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: DanCh99Posted on 2009-04-14 at 17:28:44ID: 24143876

 

by: DanCh99Posted on 2009-04-14 at 17:29:40ID: 24143880

apologies, looked again, that was IP.... sorry, pretty late round here.

 

by: BalchBinghamPosted on 2009-04-14 at 17:35:06ID: 24143906

No problem, I already found almost that exact same script on TechNet.  Works great for IP, not so good with local...

Had high hopes for the code below, but it wasn't able to remove the port either.  Still searching....

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer") 
For Each objPrinter in colInstalledPrinters
    If objPrinter.Portname = "Test" Then
        objPrinter.Delete_
    End If
Next
 
msgbox "Finished"

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: DanCh99Posted on 2009-04-14 at 17:43:13ID: 24143940

too late for me now, will check again tomorrow... sorry!

 

by: DanCh99Posted on 2009-04-14 at 17:48:24ID: 24143968

last call....:

http://support.microsoft.com/kb/321025

I know it's 2k, but most of that old good stuff still works....

 

by: BalchBinghamPosted on 2009-04-15 at 14:49:02ID: 24152907

Can you give me an example?  Does PrinterAdmin  work with XP?

 

by: BalchBinghamPosted on 2009-04-16 at 10:45:26ID: 24160329

I'm still looking for a solution that will allow me to script the deletion of a local printer port.  Any help?  It has to be scripted because it will be pushed out to 500+ computers.  Thanks in advance.

 

by: yehudahaPosted on 2009-04-21 at 03:07:42ID: 24192554

try this:

strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer")
 
For Each objPrinter in colInstalledPrinters
    If Left(objPrinter.Portname, 3) = "LPT" Or Left(objPrinter.Portname, 3) = "USB" Then
        objPrinter.Delete_
    End If
Next

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: gecko_au2003Posted on 2009-04-21 at 05:12:58ID: 24193366

Ensuring that on the objPrinter.Delete_ Line you have the underscore at the end ie

objPrinter.Delete_

as apposed to

objPrinter.Delete

Also change the DeviceID to your device id and it should work

("Select * from Win32_Printer where DeviceID = 'hp 2000c'")  

Replace hp 2000c as per above between the single quotes to what your device id is.

 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery _ 
("Select * from Win32_Printer where DeviceID = 'hp 2000c'") 
For Each objPrinter in colInstalledPrinters 
objPrinter.Delete_ 
Next 

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: BalchBinghamPosted on 2009-04-21 at 07:35:57ID: 24194874

These look like great ways to delete a printer, but I need to delete the actual port.  See the attached screen shot, I may not have been very clear in my purpose.

  • Port.JPG
    • 35 KB

    Port to be deleted is Highlighted

    Port to be deleted is Highlighted
 

by: gecko_au2003Posted on 2009-04-21 at 08:11:18ID: 24195319



FYI you can remove an obsolete/invalid printer port by deleting the appropriate registry key under

HKLM\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\

 

by: BalchBinghamPosted on 2009-04-21 at 09:21:07ID: 24196135

Good to know, but this is a local port not an IP port.  Near that same key is a listing for:

HKLM\SYSTEM\ControlSet001\Control\Print\Monitors\Local Port, but it does not contain a listing of local ports.

 

by: gecko_au2003Posted on 2009-04-21 at 09:29:02ID: 24196235

paq

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24135194.html

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
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

 

by: BalchBinghamPosted on 2009-04-21 at 09:42:10ID: 24196366

The symantics in the code below aren't correct, but I hope it atleast shows what I'm trying to do.  Anyway you could correct the verbage for me?

Also, in the kb article you cited above, only one of the persons questions were answered.  

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colInstalledPorts =  objWMIService.ExecQuery _
    ("Select * from Win32_LocalPrinterPort Where Name = 'Test'")
 
For Each objPort in colInstalledPorts 
    objPort.Delete
Next

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: yehudahaPosted on 2009-04-21 at 11:25:15ID: 24197418

where did you find the lost Win32_LocalPrinterPort ?

 

by: BalchBinghamPosted on 2009-04-21 at 16:16:32ID: 24200234

Ha, I just made it up!  (I said the syntax was wrong...)  Everything posted so far has had to do with IP Printers/Ports, and I was hoping it would help someone think of something I could use.  The WMI index is hard to search/use...  Even though it's a made up call, itt's the kind of script I need.

So is it possible to delete a local port?

 

by: gecko_au2003Posted on 2009-04-22 at 00:13:48ID: 24202000

Not sure I will have a look later although whilst your waiting you may be able to solve the issue using

http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en

wmi code creator ( Am pretty sure its that ) will test it later.

If you check the win32_printer class and the other classes that wmi uses for deleting a tcp ip printer port you may find a specific one to delete a local port.

Have been playing around with it a little bit at work but have been busy - I have this friday off so I can have a better attempt on friday as I wont be at work.

For now I hope this helps and appologise for taking a while

 

by: yehudahaPosted on 2009-04-27 at 08:14:33ID: 24242605

i searched for a solution to your problem and nothing came out without deleting the printer itself.
if another expert can prove other wise i will be more than happy to hear it.

 

by: yehudahaPosted on 2009-05-08 at 07:56:11ID: 24336658

hey

maybe i wrong but my last post is an answer to the question even if it's not a solution to complete the task asked for.

yehudaha

 

by: gecko_au2003Posted on 2009-05-08 at 08:28:05ID: 24337004

I have no objections - although I am guessing it would be more of an API that you would require most likely to be able to just delete the port I am guessing as I had the same problem using WMI with regards to deleting the whole printer to delete the port or not at all so not sure if you would be interested in this

 

by: BalchBinghamPosted on 2009-05-08 at 09:01:16ID: 24337415

I appreciate everyone's help and input.  I've done lots of testing here and it looks like leaving the port isn't going to cause any conflicts with the upgrade.  I'm not familar with API's, but if you have an example readily available I would be interested in seeing it.  Thanks again for the help, I really appreciate everyone's time!

 

by: gecko_au2003Posted on 2009-05-08 at 09:51:20ID: 24337925

You can't use an API in scripting languages its more for an actual programming language ( I am not that great with programming ) but you may be able to include it in an ocx control or dll or something along those lines and possibly call it from vbscript ?

 

by: gecko_au2003Posted on 2009-05-08 at 10:01:20ID: 24338040

http://www.sxlist.com/techref/os/win/api/win32/func/src/f15_15.htm

and

http://winapi.freetechsecrets.com/win32/WIN32DeletePort.htm

You will have to use EnumPorts to enumerate all the valid ports and use DeletePort ( this will only allow you to use a programming language not a scripting language to use the above ) to enumerate and then delete a selected port so you may be able to use vb 6 or something else to create an OCX file which you could then call from vbscript to do what you wanted ?

 

by: gecko_au2003Posted on 2009-05-08 at 10:08:29ID: 24338104

Looking around also reveals that before you delete it you will have to change the port so in essence

enumerate all valid ports

change the current port from local port that you want to delete to another port ( whether its local or not should not be an issue seeing as you just want it to select another port )

Then use Delete Port API to delete the port you want by selecting it and then if you want it to use another port you can use the change port again or you can select the correct port in the middle step

this website has a couple of examples that may lead you in the right direction

http://binaryworld.net/Main/CodeDetail.aspx?CodeId=3751

 

by: BalchBinghamPosted on 2009-05-08 at 10:13:42ID: 31570224

Thanks for everyones time and effort, I really appreciate.  

 

by: gecko_au2003Posted on 2009-05-08 at 10:31:10ID: 24338323

no problem - just a shame there is not an ocx that I could find to do it - If I get enough time I will look into creating an ocx in vb 6 that will do the above If I can figure it out and If I can I will post back but might be a while.

 

by: BalchBinghamPosted on 2009-05-08 at 11:50:07ID: 24339056

I do not have access to Visual Studio which is why I was trying to script it.  As I said earlier, leaving the port ended up not causing a conflict in our environment.  If you wanted to do it as a pet projection though that would be great and I'd be really interested in seeing it as I have no experience in making an ocx.

Thanks again,
Stephen

 

by: gecko_au2003Posted on 2009-05-13 at 00:10:36ID: 24372045

I have not had time to look into the ocx file but have found this on the randy birch site for visual basic

http://vbnet.mvps.org/index.html?code/system/addports.htm

That should get you started with regards to using vb 6 or porting it into vb dot net ( vb dot net 2008 ) which you can get a free version for.

If I get time at some point I will look into doing an ocx or something along those lines as obviously need to research into it as I have not done that before

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...