INTRODUCTION:

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! 🙂

THE IDEA:

  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.

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 scriptsound off audioThis 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).2After knowing this I  believe, now it’s easier to register a WMI event using Powershell cmdlet
    Register-WMIEvent

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

    Wait-Event

    3

  • 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.

    sound off audio.

  • NOTE: We need to access Audio API on our Windows machine, for which we’ve to add some C# code in our script. * Source

SCRIPT:

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.

sound off audio

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


My new book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at prateek@ridicurious.com

Optical Character Recognition

Subscribe to our mailing list

* indicates required