How can I access volume levels from the command line on Windows 7?

17

What I need

I'm looking for a tool or a script that allows me to access the Windows volume levels from the command line. Ideally it would allow me to get and set all volumes including application levels, but I'd settle for only getting, and only the master level. I don't mind if it uses PowerShell, WMI, JavaScript, .Net, Voodoo, or butterflies. Any solution that will just give me a number that I can further process to fit a range from 0% to 100% will do.

Put another way, the prompt parts are the black box that I need filled:

C:\> getvol master
50

or even

C:\> wmic <alias> where "device='master' and name='volume'" get name,value
Name    Value
Volume  0xDEADBEEF

I don't mind if I have to spend some time to wrap it or even write something myself if the tools to do so are freely available. In the latter case, I'd need some pointers/proof-of-concept for the core, but I can manage the cruft on my own. Also I'd like to avoid installing new development frameworks just for this purpose (note that both music2myear's and iglvzx's answers were given before I introduced these requirements).

As "platforms", I have available:

  • anything that comes with Windows 7 Home Premium
  • anything that comes with Cygwin
  • Perl
  • Python
  • Ruby

Generally, open-source solutions are greatly preferred. Closed-source tools are ok as long as they're freeware and the source can be trusted. Commercial solutions are no-go.

What I don't need

  • There are a few similar questions that mostly deal with setting the volume. Most of all, I need to get the volume, not set.
  • There is nircmdc, which supports setting volume, but not getting.
  • There is a key[1] in my registry that has values that change predictably when I adjust the volume via the GUI mixer, but I'd guess reading from that key is prone to breakage on hardware changes or system upgrades. Unless I can rely on the solution being reasonably future-proof, I can't use it.
  • There are AutoHotKey and WSH solutions for setting the volume on the Web, but a) they too are only good for setting and, worse, b) they use SendKeys or automated mouse movement to control the GUI. Automating the GUI is not an option.

The ##windows channel on freenode is unhelpful as ever, Google overflows with hacks and half-baked workarounds, and I can't tell a WinAPI call from a Wiccan incantation.

Does anyone know of a way?


  1. HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0000\MixerSettings\eLineOutTopo\PrimaryNode000
windows-7
command-line
audio
script
cygwin
asked on Super User Feb 28, 2012 by peth • edited Mar 20, 2017 by Community

3 Answers

5

This question was asked and answered on StackOverflow: https://stackoverflow.com/questions/4640106/how-to-get-current-volume-of-system-sound-device-in-windows-7

The answer: https://stackoverflow.com/a/4640225/704977

You're looking for the EndpointVolume API. This is part of the new audio APIs that were released in Windows Vista, and it can be used to get or set the master volume.
... There's a complete managed wrapper library available on CodeProject: Vista Core Audio API Master Volume Control.

answered on Super User Feb 28, 2012 by music2myear • edited May 23, 2017 by Community
4

I know you said you did not want an AutoHotkey solution, but I believe the AutoHotkey API has what you are looking for:

You do not need to create GUIs or use hotkeys with AutoHotkey. You can write powerful command line utilities by compiling an .ahk script. That being said, you will want to look at the FileAppend command for working with stdout.

answered on Super User Feb 29, 2012 by iglvzx • edited Mar 2, 2012 by iglvzx
0

The master volume is stored in the registry location:

...\#eSpeakerTopo\Properties\{7fb7b48f-531d-44a2-bcb3-5ad5a134b3dc}

For example, this REG file sets the master volume to 100%.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\DeviceClasses\{6994ad04-93ef-11d0-a3cc-00a0c9223196}\##?#HDAUDIO#FUNC_01&VEN_8384&DEV_7680&SUBSYS_83847680&REV_1034#4&31e60982&0&0001#{6994ad04-93ef-11d0-a3cc-00a0c9223196}\#eSpeakerTopo\Properties\{7fb7b48f-531d-44a2-bcb3-5ad5a134b3dc}\20000]
@=hex(ffff1003):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
  00,00,00

The key name may differ. You need to restart for changes to take effect.

You may not be able to edit or open the Properties or {7fb7b48f-531d-44a2-bcb3-5ad5a134b3dc} key in regedit. Although you have the necessary access permissions, you may receive an error like this:

[Window Title]
Error Opening Key

[Content]
{7fb7b48f-531d-44a2-bcb3-5ad5a134b3dc} cannot be opened.
An error is preventing this key from being opened.

[OK]

This is because the key name is too long. You will have to temporarily rename the long key name to one character and then rename it back.

answered on Super User Dec 3, 2015 by XP1

User contributions licensed under CC BY-SA 3.0