This is a blogpost about a script to set drain mode for all session hosts via powershell in one WVD hostpool. This is a script for the spring release. The fall release CMDLets of WVD uses other CMDlets.

WVD Script

Firstly, we need to connect to Azure using Powershell. We can do this by using the following command:

After that you need to make sure you are using the correct Azure subscription in context. You can make sure by using this command:

Next, we can run the “WVD set drain mode Powershell script”:

The update-AzWvdSessionHost CMDlet is used. You can read more about it here.

When you run the script the output is shown per Session Host and looks like this:

WVD set drain mode powershell output

The for-each loop shown in the script example is somewhat special and needs some explanation. This the for-each loop:

We need to split the array that contains the string which is the name of the WVD Sessionhost. The original name of this value is. For example:


When you use this to disallow session you will see this error:

Error for setting drain mode in powershell

We need to make sure that only this name remains:

So we need to split the array at the “/”. We are achieving this by using this piece of code:

This puts the hostname of the WVD Session Host in the “$WVDArray[1]” array to make sure the correct name is selected and can be found as a session host.

After this you can add this script in to your Azure DevOps configuration to help you simplify your WVD Deployment!


Microsoft docs about update-wvdsessionhost

Split array in Powershell

Blog about Azure information Protection

Related Posts

12 thoughts on “WVD Set Drain Mode Powershell

  1. Pingback: essay
  2. thanks for sharing, I have follow your script but how I can run it through Azure Automate or any other which run every evening automatically to enable the drain mode?

    1. Hello,

      You can create an Azure Automation account and give the Service Principal from that Azure Automation account permissions on your subscription. After that you can run this script via the Azure Run As option in the Runbook of the automation account.

      Kind Regards,


  3. Hi Niels,
    You helped me before 😉 hope you have an answer to my question.
    How can I set drain mode on on newly released AvD VM’s in DevOps.
    I don’t want all sessionshosts in the Hostpool in drain mode, only the new machines.
    So I’m looking for a pipeline variable that only contains the VM’s that are released.
    Do you know if this variable?


    1. Hi Floris,

      Thanks for your response. I think you should add a tag which holds the date (and maybe the time) that you deployed the VM’s. You can add these tags via the ARM or BICEP template as variables in your deployment pipelines. After that, you could select the newly created VM’s via the tag that you added with the date.

      Hope this is what you need.


Leave a Reply

Your email address will not be published. Required fields are marked *