Question

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?

Was it helpful?

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

OTHER TIPS

The best way I can see for manipulating the system volume level on Windows without the need for installing additional software is to use VBScript in one of the following ways:

Toggle muted: (already mentioned in a previous answer)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

Increase volume level:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAF))

Decrease volume level:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAE))

This should work for most modern Windows machines. I've tested this on Windows 7 and 8.1 and it works fine even when run with "do as" in LC, so it is possible to embed these scripts within an executable and run them without the need of saving them as individual files.

On Windows 7 I could do this by controlling the keystrokes using WScript.

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 
WScript.Sleep 1500 
oShell.SendKeys"{TAB} " ' Tab to the mute and press space
oShell.SendKeys"%{F4}"  ' ALT F4 to exit the app.

I saved it in to a file called Mute-Sound.vbs and created a shortcut on the desktop to assign a shortcut key. CTRL+ALT+F12. For some reason the keyboard shortcuts only work if they are on the desktop so I am not sure how reliable keyboard shortcuts are!

Misty Manor's Answer Works on Windows Vita as well. This, i literally just made, does the same in a sense.

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 'Runs The Master Volume App.
WScript.Sleep 1500 'Waits For The Program To Open
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20, If It Is Muted Then It Will Unmute It
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys"%{F4}"  ' ALT F4 To Exit The App.

If you want to decrease the volume you would do

oShell.SendKeys("{PGDN}") 'It Will Decrease The Volume By 20

http://www.nilpo.com/2008/11/windows-xp/mute-sound-volume-in-wsh/ He uses a keystroke of a Multimedia Keyboard mute key. Clever ;-)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

From within a browser window/web page - definitely no way at all. Browser sandbox security would prohibit it even if it were technically possible.

From within Windows Script Host (standalone .vbs or .js file) - no way that I am aware of. WMI does not provide control over this either, according to this question.

I used nircmd.exe along with vbscript to control system volume.

Set objShell = CreateObject("WScript.Shell") 
If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If
objShell.Run("nircmd.exe setvolume 0 0 0") 

'Set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.Popup "Success", "5", "MuteSpeaker"

' to successfully run this vbscript during log-off, nircmd.exe during should be present in "C:\Windows\System32"
' [cscript //X scriptfile.vbs MyArg1 MyArg2]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top