Auto Mute Powershell

  • Open Notepad and paste the script
  • Save as AutoMute.ps1 (select "All Files" type).
  • Right-click the file → Run with PowerShell (it runs in a loop).
  • For it to start automatically, create a Task Scheduler task that runs the script at logon (hidden).
# PSAutoMute.ps1
# Automatically mutes speakers when headphones are unplugged

[cmdletbinding()]
Param()

# Adding definitions for accessing the Audio API
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;

[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
    // f(), g(), ... are unused COM method slots. Define these if you care
    int f(); int g(); int h(); int i();
    int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
    int j();
    int GetMasterVolumeLevelScalar(out float pfLevel);
    int k(); int l(); int m(); int n();
    int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
    int GetMute(out bool pbMute);
}

[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
    int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}

[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
    int f(); // Unused
    int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}

[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }

public class Audio {
    static IAudioEndpointVolume Vol() {
        var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
        IMMDevice dev = null;
        Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
        IAudioEndpointVolume epv = null;
        var epvid = typeof(IAudioEndpointVolume).GUID;
        Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
        return epv;
    }

    public static float Volume {
        get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
        set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
    }

    public static bool Mute {
        get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
        set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
    }
}
'@ -Verbose

While ($true) {
    # Clean all events in the current session
    Get-Event | Remove-Event -ErrorAction SilentlyContinue

    # Register for device change events and wait
    Register-WmiEvent -Class Win32_DeviceChangeEvent
    Wait-Event -OutVariable Event | Out-Null

    $EventType = $Event.sourceargs.newevent | 
        Sort-Object TIME_CREATED -Descending | 
        Select-Object EventType -ExpandProperty EventType -First 1

    # EventType 3 = Device Removed (unplug) → Mute
    # EventType 2 = Device Arrival (plug in) → Unmute (if currently muted)
    If ($EventType -eq 3) {
        [Audio]::Mute = $true
        Write-Verbose "Muted [$((Get-Date).tostring())]"
    }
    elseif ($EventType -eq 2 -and [Audio]::Mute -eq $true) {
        [Audio]::Mute = $false
        Write-Verbose "UnMuted [$((Get-Date).tostring())]"
    }
}