Powershell Auto Mute, when headphones are accidently unplugged.


Do you know mobile phones have an inbuilt feature to Auto mute a playing song/music,  if by mistake you plug out your earphones from the jack, that prevents your phone to keep playing the music on speakers, which could be quite embarrassing in some surroundings and situations, but unfortunately we don’t have such a feature available on our desktops/laptops 😦

And I was victim of such an embarrassing situation a few days back, when I was listening to some music on my developer workstation in my office with my headphones on and tried to rest my head on the chair’s backrest in relaxing position, due to tension the cable got unplugged from headphone jack and allowed the music to be played for a while from my laptop speakers and it took me few seconds to realize that I screwed it and everybody in the office is  looking at me 😀 . Somehow in hurry I managed to mute my PC. Phew!

It was embarrassing but the good part was I was at least not listening to Taylor Swift 😛 , So I thought why not to look for a solution available to fix this for me? Really I didn’t care much about finding one but was more interested in making one with Powershell! 🙂


  1. Run a background PowerShell script (Hidden preferred) always when your machine is up
  2. That monitors a device plug – IN/OUT events
  3. If a Device Plug OFF event is triggered, make sure to MUTE your machine
  4. Or, in the case of Plug-IN event is generated UNMUTE the machine.


  • Running the script in background:  We can create a basic Task in Windows task scheduler to run a PowerShell script whenever machine whenever computer starts to run a PowerShell script1This will make sure our script is always running in the background, waiting for a device to be plugged In/out
  • Capture Device Plug-In/Out Events:  After some googling and research I found a WMI class Win32_DeviceChangeEvent that represents device change/modification events on a windows machine. Here  is the link to MSDN Documentation of the WMI class.It clearly mentions that, Event Type 3 generated under this class is Device Removed and Event Type 2 is Device arrival (plugged in).2

    After knowing this I  believe, now it’s easier to register a WMI event using Powershell cmdlet


    and wait for any of the above events (Event Type 2 or 3) to occur using cmdlet



  • Handling the events: In the case  Event type 3 is triggered, which as per Microsoft’s documentation means – A device plugged off (Removal) event, then we should MUTE our Machine, and when the device is plugged in (Arrival) event is generated we need to UNMUTE the machine.3.
  • NOTE: We need to access Audio API on our Windows machine, for which we’ve to add some C# code in our script. * Source


Wrapping up everything in a PowerShell script looks something like this

You can also fork my Github repo of the script here and feel free to contribute 🙂


You can run the script on demand like in the below animation or you can create a task in task scheduler to make it run (Hidden) always when your machine starts.


Hope you’ll find the script useful or at least gain some knowledge. Thanks for reading, Cheers! 🙂



9 thoughts on “Powershell Auto Mute, when headphones are accidently unplugged.

  1. I find it odd that this isn’t something for this in windows settings already. I have ubuntu dual booted on my machine and noticed that this functionality was on by default.


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