Search code examples
windowsvbscriptjavascriptvolumemute

How to control Windows system volume using JScript or VBScript?


I want to control the volume of my Windows system from a JScript or VBScript script. Any ideas?

Also, can I unmute the system volume if it is muted?


Solution

  • To mute or unmute the system volume, you can simulate the Mute key press using the WshShell.SendKeys method:

    var oShell = new ActiveXObject("WScript.Shell");
    oShell.SendKeys(Chr(&HAD));
    

    As for changing the volume level from a script, there's a solution that involves some Windows automation, such as launching the System Volume applet and simulating the appropriate keyboard shortcuts in it, but I don't think it's reliable. Therefore I recommend that you use some external utility capable of changing the volume level, and call it from your script. For example, you could use the free NirCmd tool:

    var oShell = new ActiveXObject("WScript.Shell");
    
    // Increase the system volume by 20000 units (out of 65535)
    oShell.Run("nircmd.exe changesysvolume 20000");
    
    // Decrease the system volume by 5000 units
    oShell.Run("nircmd.exe changesysvolume -5000");
    

    NirCmd can also mute or unmute the system volume:

    var oShell = new ActiveXObject("WScript.Shell");
    oShell.Run("nircmd.exe mutesysvolume 0");  // unmute
    oShell.Run("nircmd.exe mutesysvolume 1");  // mute
    oShell.Run("nircmd.exe mutesysvolume 2");  // switch between mute and unmute