How to Use PowerShell to Disable and Enable Webcam on Windows 10
While using laptops, sometimes you may want to disable the built-in webcam for some reasons, such as security or debugging webcam related programs. Not like a USB webcam, the built-in camera is unpluggable. Therefore, a general way is to open the device manager, find the camera, and disable it. To save time, we can create PowerShell scripts to toggle camera status quickly.
How to Disable / Enable Webcam with PowerShell Scripts
Run the Windows Powershell command-line tool as administrator.
We can use Get-PnpDevice cmdlet to enumerate all devices known to PnP (Plug and Play).
Since I just want to list webcam devices, I can filter the devices by name:
Get-PnpDevice -FriendlyName *webcam*
As you can see, I have two webcams: a built-in USB webcam and a Logitech C920 webcam.
The Logitech C920 webcam also supports audio input which should not be listed here. Therefore, I need to take a further step to filter the results by adding the class name:
Get-PnpDevice -FriendlyName *webcam* -Class Camera,image
The Logitech C920 webcam is physically removable, and thus my target is the built-in XiaoMi USB 2.0 Webcam.
Use Disable-PnpDevice cmdlet to disable the built-in webcam by instance Id:
Disable-PnpDevice -InstanceId (Get-PnpDevice -FriendlyName *webcam* -Class Camera -Status OK).InstanceId
Once a device is disabled, its status will be changed to Error. To make the webcam work again, use Enable-PnpDevice cmdlet. Change the current status from OK to Error:
Enable-PnpDevice -InstanceId (Get-PnpDevice -FriendlyName *webcam* -Class Camera -Status Error).InstanceId
I can use the online webcam app to check the camera status as I execute the commands.
Finally, in order to quickly run the scripts, save the commands to disable.ps1 and enable.ps1 files.
By default, a *.ps1 file will be opened in Notepad when we double-click it. To run the script directly, I created two batch files and corresponding desktop shortcuts. We have to use an absolute path, not a relative path for finding the ps1 file.
// disable.cmd
powershell -file d:\disable.ps1
// enable.cmd
powershell -file d:\enable.ps1
Right-click a shortcut file and go to Properties > Advanced. Check “Run as administrator”.
Now I can double-click the batch files to toggle webcam status.
Source Code
https://gist.github.com/yushulx/27ecfc190ed091dc6373e43c6558e582