33

Is it possible to change the volume in Windows XP via the command line?

underskor
  • 584

6 Answers6

45

NirCmd is an application that does that and more.

Example of use:

  • Increase the system volume by 2000 units (out of 65535)
    nircmd.exe changesysvolume 2000
  • Decrease the system volume by 5000 units (out of 65535)
    nircmd.exe changesysvolume -5000
  • Set the volume to the highest value
    nircmd.exe setsysvolume 65535
  • Mute
    nircmd.exe mutesysvolume 1
  • Unmute
    nircmd.exe mutesysvolume 0
harrymc
  • 498,455
13

Having read these posts and having looked for alternatives I decided to write my own command line utility, called SetVol, to set the volume. It works a little more simply than what is described in some of the other posts on this page, here are some examples that you would enter at the command prompt:

setvol 75
setvol -10
setvol +12
setvol mute
setvol unmute

There are other options too. I've released it as freeware, and you are welcome to visit www.rlatour.com/setvol for more information and to download a copy.

Enjoy

Rob
  • 576
10

Make the JavaScript files:

echo var oShell = new ActiveXObject("WScript.Shell"); >> volup.js<BR>
echo oShell.SendKeys(String.fromCharCode(0xAF)); >> volup.js

echo var oShell = new ActiveXObject("WScript.Shell"); >> voldown.js<BR>
echo oShell.SendKeys(String.fromCharCode(0xAE)); >> voldown.js

echo var oShell = new ActiveXObject("WScript.Shell"); >> togglemute.js<BR>
echo oShell.SendKeys(String.fromCharCode(0xAD)); >> togglemute.js

Show the volume control, so you can see what you're doing:

sndvol
(or maybe sndvol32)

Change the volume:

cscript voldown.js

Note: I've had this approach work reliably on machines that I've attempted to use it on. Helen's answer to Sibo Lin's StackOverflow question about this indicates muting isn't reliable, but volume-changing may not be quite as reliable. I suspect that the level of reliability may be different on different machines. This approach is using the technology of mimicking a keystroke, and specifically a volume control key on an enhanced media keyboard. At the time that Windows started supporting this, such a keyboard was basically a little-used frivolous feature that offered functionality that was previously available only with custom drivers. It wouldn't surprise me if this code was less polished, and less likely to work on some of the various (perhaps older) hardware that's out there. All that said, I haven't had troubles with it myself.

Credits:

One caveat: This question has been tagged with Windows XP. I just tried this in Windows 10. I know I've used this in Windows 7 (at least enough to test it out). As I first started to see Microsoft's built-in support for these enhanced keyboards around the time of Windows ME, I would think this is likely to work well win WinXP too. I don't recall if I actually tested this with that operating system. Still, if this approach doesn't work well, I don't expect it to cause problematic side effects.

phuclv
  • 30,396
  • 15
  • 136
  • 260
TOOGAM
  • 16,486
4

Here's an awesome powershell script found here: (Tested on Windows 10 and 8.1 x64)

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)); }
   }
}
'@

Now to set volume:

[audio]::Volume = 0.4  # 40%
[audio]::Volume = 0.78 # 78%

And to (un)mute:

[audio]::Mute = $true  # Mute
[audio]::Mute = $false # Unmute
wasif
  • 9,176
3

Owing and further simplifying Nircmd

@echo off
rem 65536 is 100%
rem device where zero is the default device
rem left and right
rem supports whole numbers only therefore throws "missing operator" error when specifying 655.36
set /a volume=%1 * 655
nircmd setvolume 0 %volume% %volume%

save it as a .bat file and execute by passing a parameter

eg: sound 60 will set the the sound volume 60%

make sure you put the bat file either next to the nircmd or in %windir% (or define its folder in %path%)

0

NirCmd has commands for a specific running app, like:

NirCmd.EXE SetAppVolume  EPyrus.EXE .25 & Rem Set    EPyrus volume to 25 percent
NirCmd.EXE MuteAppVolume EPyrus.EXE  2  & Rem Toggle EPyrus "mute" state

See the author's blogpost

Bilbo
  • 1,036