# Display the Audio Sound-Level Window

**URL:** <https://forums.powershell.org/t/display-the-audio-sound-level-window/25386>\
**Category:** PowerShell Help\
**Created:** [January 31, 2025, 6:53pm UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386 "2025-01-31T18:53:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marceepoo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/marceepoo/32/4120_2.png) [@marceepoo](https://forums.powershell.org/u/marceepoo)\
**Post date:** [January 31, 2025, 6:53pm UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386/1 "2025-01-31T18:53:14Z")

</div>

I’m writing a PS script that will toggle the sound level between 20% and 100%.

I would like to add a line that opens the “Display Audio Sound-Level Window” shown in the image below after the script runs so I can see which sound level I got at the end.

 ![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/1/1787b933de85be609f14f06d8c4fe42fb60b0f03.png)  
But I cannot find a command line or PS utility that opens the Sound-Level Window.

Someone suggested I use the `sndvol.exe` utility, but it:

1. Displays the sound index using small digits that my aging eyes would have to hunt for; and
2. I’d have to manually close the window.

By contrast, the “Display Audio Sound-Level Window”:

1. Displays the sound level using large digits that are easily readable, and
2. Will close automatically when I hit any key or use my mouse in another window:

Does anyone know if there’s a PS way to open the “Display Audio Sound-Level Window”?

```
                 Marc

```

---

<div class="post-metadata">

**Author:** ![vern\_anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vern_anderson/32/5140_2.png) [@vern\_anderson](https://forums.powershell.org/u/vern_anderson)\
**Post date:** [January 31, 2025, 8:49pm UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386/2 "2025-01-31T20:49:04Z")

</div>

I made this based off what I learned in this thread

> <https://stackoverflow.com/questions/21355891/change-audio-level-from-powershell>

```auto
Param ([Parameter(Position=0)][ValidateRange(0, 100)][double]$Percent,[Switch]$Mute)
$Decimal = $Percent / 100
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)); }
    }
}
'@

if ($Mute) {[audio]::Mute = $True} ## [audio]::Mute = $true # Set to $false to un-mute
else {[audio]::Mute = $False ; [audio]::Volume = $Decimal ; Write-Host -Object "Sound volume is now at $Percent percent" -ForegroundColor Green}

```

it could be easily modified to do a “GET” basically the “[audio]::Volume” by itself will contain the current value. The problem is it is in decimals. In other words 50 % volume is 0.5 for some reason. So 100 % volume would be 1.0. Good luck have fun with it!

---

<div class="post-metadata">

**Author:** ![marceepoo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/marceepoo/32/4120_2.png) [@marceepoo](https://forums.powershell.org/u/marceepoo)\
**Post date:** [February 13, 2025, 6:58pm UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386/3 "2025-02-13T18:58:12Z")

</div>

Thank you. I appreciate the substantial effort you made to help me. Alas, I’m much too much of a PS novice to understand the code, or even how to use the script. I’ve used param to send string arguments to a script, but I don’t understand:

1. The two bracketed parentheticals, ‘[Parameter(Position=0)][ValidateRange(0, 100)]’, and/or
2. What I should use to send an argument to the parameter, ‘[double]$Percent’,
3. What I should use to send an argument to the parameter, ‘[Switch]$Mute’

Could I impose on you to give me some guidance about how to use the script?  
Thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [February 14, 2025, 5:50am UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386/4 "2025-02-14T05:50:34Z")

</div>

Run the functions to define them first.

```auto
function Get-Volume {
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)); }
    }
}
'@

    return [PSCustomObject]@{
        Volume = [math]::Round([audio]::Volume * 100)
        IsMuted = [audio]::Mute
    }
}

function Set-Volume {
    [cmdletbinding()]
    param(
        [validaterange(0,100)]
        $Percent,

        [bool]$Mute
    )

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

    if($Percent){
        [audio]::Volume = ($Percent / 100)
    }

    if($null -ne $Mute){
        [audio]::Mute = $Mute
    }

    return [PSCustomObject]@{
        Volume = [math]::Round([audio]::Volume * 100)
        IsMuted = [audio]::Mute

    }
}

```

Then call them like this

```auto
Get-Volume

Set-Volume -Percent 40
Set-Volume -Mute $true
Set-Volume -Mute $false

```

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/e7ae6555a11de0a4ac9b4454b1c6ce8e326dfef8.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 16, 2025, 5:50am UTC](https://forums.powershell.org/t/display-the-audio-sound-level-window/25386/5 "2025-03-16T05:50:57Z")

</div>

Closed on March 16, 2025, 5:50am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
