Posted in Computers and Internet

Tip- Disable/Enable web cam at will by PowerShell


Here is the one-line code to enable or disable the web cam when you want. Here is how it works.

I wrote a one line code and save it as a PowerShell (.ps1) file. Created a batch file to call the PowerShell script files to enable or disable the web cam.

Prerequisite

Open PowerShell and type (or copy/paste). This will display all of your Camera device(s).

Get-PnpDevice | ? {$PSItem.Class -eq 'Camera'} | fl FriendlyName,InstanceId
Here is the example:
E:> Get-PnpDevice | ? {$PSItem.Class -eq 'Camera'} | fl FriendlyName,InstanceId
 FriendlyName : Integrated Camera
 InstanceId   : USB\VID_04CA&PID_703C&MI_00\7&36726615&0&0000
 FriendlyName : S-YUE 8MP USB Camera
 InstanceId   : USB\VID_0BDA&PID_5075&MI_00\8&3E5D74C&0&0000

Now note down the Instance ID of the current camera (copy it to the clipboard).

Creating the PowerShell and Batch files

Disable Script

  1. PowerShell script file

Type the command below in a Notepad/Editor and replace the InstanceID you noted down above.

Disable-PnpDevice -InstanceId 'USB\VID_0BDA&PID_5075&MI_00\8&3E5D74C&0&0000' -Confirm:$false

Save the file as Disable-Camera.ps1.

2. Batch file

Type the command below in a Notepad/Editor and replace the path of your Powershell Scirpt file. Save the file as Disable-Camera.bat

PowerShell.exe -Command "C:\Temp\Disable-Camera.ps1"

Enable Script

  1. PowerShell script file

Type the command below in a Notepad/Editor and replace the InstanceID you noted down above.

Enable-PnpDevice -InstanceId 'USB\VID_0BDA&PID_5075&MI_00\8&3E5D74C&0&0000' -Confirm:$false

Save the file as Enable-Camera.ps1.

2. Batch file

Type the command below in a Notepad/Editor and replace the path of your Powershell Scirpt file. Save the file as Enable-Camera.bat

PowerShell.exe -Command "C:\Temp\Enable-Camera.ps1"

How to use it?

Here are my created files:

I made the shortcut for the Batch file on my desktop. When I needed, I right-click on the shortcut icons, select Run as Administrator.

To enable or disable the device, script has to run in elevated PowerShell. That’s why you have to run the shortcut Run as Administrator.

Better yet, I use Circle Dock to quickly launch programs. So I created two new icons for it.

Author:

Being in Silicon Valley for years, I became a IT Infrastructure Geek by experience and surrounded by other Geeks everywhere. I try to help others by the solutions I found on odd occasions.

One thought on “Tip- Disable/Enable web cam at will by PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s