Too many serial ports, need to remove them

I work for a company that does mobile phone (cellphone) unloicking - i.e. unlock to all networks. As part of this, they use cables that connect from the various models of phone to the PC via USB. They register as COM devices in Windows. The problem is that because each phone has a different serial number (probably the IMEI of the phone), every phone appears as a new serial device. This means that his unlock machine has over 150 serial port devices that are just sitting around and will never be used again. The software he uses cannot access port 200+, so every unlock he does he has to remove an old unused COM device, plug in the new one, then switch the COM port number of the new device to whatever the one was that he removed. For example, if he removed COM142, he adds the new device and changes the port number to COM142. It's getting rediculous.

I know that Windows XP stores COM device information in the registry, and would like to know if there's anything I can do to remove them all. I can program in VB.NET and C#, so if it's a case of conditionally removing registry entries I'm happy to do so. I have a virtual machine set up to allow me to revert to a snapshot in case I mess up the registry.

Any ideas how I can remove the ports?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

go to device manager>view tab
click "show hidden devices" and delete them
that should do it
burningmaceAuthor Commented:
Yeah, I know you can do that. Problem is there's like 200 of them and I want to remove them programatically.
then i can't help you
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Gary CaseRetiredCommented:
My code-writing days are long over ... but if I was going to do this, here's the approach I'd take, which should let you do what you want to do:

=>  Install one of the applications that track registry changes, such as the free RegRunner:

=>  With RegRunner running, go to Device Manager and delete one of the ports.   You'll now know exactly what keys are modified in the registry ... so it should be fairly simple to write a program to make the modifications you want programmatically.    ... you may need to do a couple of deletions to identify the exact pattern; but I suspect it will be fairly obvious.

=>  To make this question useful for any future EE members with a similar issue, please post your results here :-)
You could download devcon, a command line tool for the Device Manager, and do the following:
  1. Create a process the calls devcon with the parameter as "findall =ports"
  2. Setup the process so that we can get the output of it
  3. Loop through each line of the output, and get the port DeviceID & Name
  4. For each port you want to remove, call devcon uninstall "@<Device ID>"
Devcon can be downloaded here:

Also, I'll add some code on how to automate this, as it's something I'll find quite useful to have too.
Here we go, I copied the devcon executable to c:\windows\system32

Note, as the Device ID may contain &, which is a special character in the shell, the Device ID needs to wrapped in quotes. Also it needs to be prepended by @, I don't know why, but it's done this in the MS example. If it's not prepended with @, the Device ID won't be matched, and nothing will happen.
      string pathToDevCon = @"C:\windows\system32\devcon.exe";
      /* Create the Process to retrieve all the ports
       * StartInfo is modified so that we can capture StdOut
      Process ps = new Process();
      ps.StartInfo = new ProcessStartInfo(
        pathToDevCon, "findall =ports");
      ps.StartInfo.RedirectStandardOutput = true;
      ps.StartInfo.UseShellExecute = false;
      /* Read all the ports into a dictionary object:
       * Output from devcon is in the format <Device ID>        : <Description>
       * Final line lists number of devices returned
      Dictionary<string, string> ports = new Dictionary<string, string>();
      do {
        string line = ps.StandardOutput.ReadLine();
        if (line == null) break;
        string[] parts = line.Split(':');
        if (parts.Length == 2) {
          ports.Add(parts[0].Trim(), parts[1].Trim());
      } while (true);
      // Action to take, can be {uninstall | disable | enable}
      string action = "disable";
      // Loop through each device, and 
      foreach (string devID in ports.Keys) {
        bool doAction = true; // Use your own logic here to decide whether to remove the device 
        if (!doAction) continue;
        // Execute the action on the Device ID, again capturing StdOut
        Process psAction = new Process();
        ps.StartInfo = new ProcessStartInfo(
          string.Format("{0} \"@{1}\"", action, devID));
        ps.StartInfo.RedirectStandardOutput = true;
        ps.StartInfo.UseShellExecute = false;
        /* Echo only the 1st line of StdOut from the Process as the 2nd line
         * returns the number of devices affected

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burningmaceAuthor Commented:
I've had to make a few alterations to your code. You defined psAction then use ps instead, which was probably a typo. I've also modified the code to only process COM ports by checking if the device name begins with the string "Communications Port (COM".

I'm going to the store tomorrow so I'll test it out in situe.
burningmaceAuthor Commented:
Excellent. With a few minor modifications this worked perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows XP

From novice to tech pro — start learning today.