How can I use powershell to set the speaker volume? Ive dug around on here and elsewhere online can cant really find an answer.
I think I will have to write something in C# that wraps a Win32 API and THEN call it from my powershell script. The Win32 API's would be one of these
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
Instead of twiddling your thumbs as you wait for a long-running script to complete, you can have PowerShell play a sound to alert you when it's done.
As a scripting language, PowerShell is commonly used for automating the management of systems. It is also used to build, test, and deploy solutions, often in CI/CD environments. PowerShell is built on the . NET Common Language Runtime (CLR).
Here is a PowerShell function you can run to set the Audio Level from 0–100%. How does it work? PowerShell is creating a COM Object of the type Windows Shell, running the function SendKeys () with the parameters [char]173. What [char]173? This represents the 173th Unicode character value for the Mute Toggle key in Windows. 1.
It seems that no PowerShell command codes could use to modify sound volume up to maximum only. The easy answer may use Nircmd to modify sound volume. Thanks, Snakefist. I heard the application program, but I wish to use PowerShell to modify SndVol.exe only. I will try to use nircmd to modify sound volume. Best regards. You are welcome.
Yes, Windows PowerShell can help in many different ways in looking at audio drivers. The first thing to do is to find the audio device. To do this, use the WMI class Win32_SoundDevice WMI class.
Starting with Vista you have to use the Core Audio API to control the system volume. It's a COM API that doesn't support automation and thus requires a lot of boilerplate to use from .NET and PowerShell. Anyways the code bellow let you access the [Audio]::Volume and [Audio]::Mute properties from PowerShell.
SendKeys stopped working for me in Windows 10 (it literally types digits where my caret is). I found this blog post with a very convenient way of doing it.
First, run this to get access to 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)); }
}
}
'@
Then control the volume like this:
[audio]::Volume = 0.2 # 0.2 = 20%, etc.
And mute/unmute like this:
[audio]::Mute = $true # Set to $false to un-mute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With