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! 🙂
- Run a background PowerShell script (Hidden preferred) always when your machine is up
- That monitors a device plug – IN/OUT events
- If a Device Plug OFF event is triggered, make sure to MUTE your machine
- Or, in the case of Plug-IN event is generated UNMUTE the machine.
MAKING IT WORK:
- 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 script
This 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).
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.
- 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 🙂
HOW TO RUN:
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! 🙂