In Device Manager, I can right click on a serial device, navigate to the Port Settings tab, click Advanced, and change the COM Port Number via a drop-down menu.

I need to automate this step across a large number of PCs. Is there a way to script this process, or to do it programmatically?

  • Are they all the same version of Windows? – RockPaperLizard Dec 11 '14 at 6:46
  • They are not - could be WinXP, Win7, or Win8. Having said that, I'll happily accept a solution that only works on one (better than nothing!) – Matthew King Dec 11 '14 at 6:51
  • Hmmm....curious what is the specific application for this process? – mdpc Dec 11 '14 at 7:22

This question is probably best asked on stackoverflow. That said, take a look at ComDBReleasePort and ComDBClaimPort in the COM Port Database. However, changing COM port assignments programmaticly is not exactly considered a best practice. For example, what happens if two more more concurrent applications are fighting to get the same COM assignment?

Windows dynamically assigns COM port numbers for a good reason. Ideally, your application should be written so that it searches for the specific COM port it needs. This can be done via polling or by using the Windows Management Interface (WMI).

If you're referencing some application that someone else wrote, and you're just stuck with someone else's program design (or lack thereof), then you may want to consider adding a batch script to modify the registry key under HKLM\Hardware\DEVICEMAP\SERIALCOMM at startup, since the new value(s) are not likely to hold between re-boots.

I don't know how how well this last option would work, but if you're a serious PowerShell or Python user, you could theoretically use either scripting language to directly access the APIs mentioned at the beginning of my response for a one-time COM port re-assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.