How can I programmatically disable or enable a device adapter on Windows instead of using Device Manager?

7 ビュー (過去 30 日間)

I am encountering an issue that requires me to disable or enable a device adapter on Windows in a MATLAB script. Is there a way to do this?

For example, when programming an RF device with an adapter, I have found that the controlling PC causes interference patterns with the measurements. If the adapter is disabled in Windows Device Manager, the issue is resolved. Can I disable and enable the adapter programmatically so it doesn't affect my measurements?

採用された回答

MathWorks Support Team
MathWorks Support Team 2024 年 3 月 21 日
To disable and enable device drivers programmatically on Windows, you can use PnPUtil, which is a driver-managing tool from Microsoft included in every version of Windows. It can be found at the following link:
To use this tool in a MATLAB script, you can use the "system" command to programmatically disable or enable the device by using the following command after replacing "deviceID" with the actual device ID (keep the double quotes):
>> system('pnputil /disable-device "deviceID"')
To get the actual device ID, follow these steps:
1. Find the hardware ID in Windows Device Manager > the device name > Properties > Details > Hardware IDs (in the dropdown menu). It should be the first ID if there are multiple in the list, and is generally the longest ID.
2. Run the following command in Command Prompt (run as Administrator), replacing "hardwareID" with the hardware ID found in the following step (keep the double quotes). 
>> pnputil /enum-devices /deviceid "hardwareID"
3. Use the "system" command above this list using the “Instance ID” field as the device ID. If successful, it should say “Device disabled successfully”.
If it tells you to reboot your device to complete configuration operations, run the command in Command Prompt (run as Administrator) and reboot when necessary. After this, it should likely be able to run the command without rebooting.
4. To re-enable the device, you can use the same “system” command except use “/enable-device” instead.
To successfully run this command, you must run MATLAB as administrator, or you can use the “runas” Windows command in the "system" call to run the command with elevated privileges.
Option 1 (Run MATLAB as Administrator):
To make MATLAB always run with administrator privileges, refer to the following MATLAB Answers post:
Note: If you run MATLAB as administrator, this may cause issues connecting to network drives. You can solve this by running the following command in Command Prompt (run as Administrator), which allows admin users to view network drives created by regular users:
>> reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v "EnableLinkedConnections" /t REG_DWORD /d 0x00000001 /f
Option 2 (Using "runas" Command):
If you would not like to run MATLAB with administrator privileges, you can use the "runas" Windows command. The Microsoft documentation for the "runas" command can be found at the following link:

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by