QuickPost: How to automatically mute the volume on XenDesktop sessions (or set it to a specific percentage)

By James Rankin | 3rd November 2015

Recently I was deploying a few XenDesktop instances into thin clients on open-access (library) areas. One of the requirements was to automatically mute the volume on these Windows 10 machines so people couldn’t disturb their neighbours.

Naturally the first suggestion was to simply disable or stop the Windows Audio Service, but we wanted users to be able to use headphones and the like, not prevent them from having sound altogether. The idea was that the default setting would always be muted – users could then turn the mute setting off manually when they plugged in their headphones or headsets.

We could have set the volume to muted in the default profile on the images also, but given that we were using local profiles if the user changed the mute settings this would then have persisted, so it was also not an option.

As to how to achieve this programatically, well of course a bit of PowerShell would do. This is what I used (not my own code, this was unearthed by the power of Google-fu) – lines may wrap:

Add-Type -TypeDefinition @’
using System.Runtime.InteropServices;
[Guid(“5CDF2C82-841E-4546-9722-0CF74078229A”), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
  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)); }
[Audio]::Mute = $true

I put this together in an AppSense Desktop Created Action, but as usual, you can deploy this any way you should so desire.

When the users logged in to their XenDesktop sessions next, the volume was automatically muted and their neighbours could continue to work in peace 🙂

On the flip side, if you wanted to set the volume to unmuted and 100% (or any specific percentage, maybe for lecture theatre machines and the like), then replace the last line above with these

[Audio]::Volume = 1.0

[Audio]::Mute = $false

Obviously if you wanted 50% instead of 100%, change the 1.0 to 0.5 – you get the idea.

Back to Blogs

Get in touch

Please call us on 0191 481 3446 or email and we’ll get back to you.

Chat to our team Messenger