Managing Azure Windows Virtual Desktop using PowerShell

Azure Windows Virtual Desktop service is designed to provide app virtualization services. Windows Virtual Desktop, sometimes referred to as WVD, needs to be managed using PowerShell modules. While you can create host pools and deploy session hosts using Azure Portal, when it comes to managing user sessions and adding or removing users to the WVD tenant, you will be required to use Windows Virtual Desktop PowerShell Modules. For example, when disconnecting a user session, you will be required to use Invoke-RdsUserSessionLogoff cmdlet and so on. This article provides some of the PowerShell commands that you might find useful in your daily WVD operational tasks.

Note: There are two versions of Windows Virtual Desktop available: WVD 1.0 and WVD 2.0. Microsoft recently released WVD 2.0. This article focuses on PowerShell modules to be used with WVD 1.0. Since many of the customers have deployed WVD 1.0, I thought it would be useful for WVD admins who are looking for some PowerShell

Installing PowerShell modules

Before you can use the PowerShell cmdlets explained in this article, you need to install WVD PowerShell modules on your computer. To install WVD PowerShell Modules, simply execute this command from an elevated PowerShell Window:

Install-Module <em-Name RDInfra.RDPowershell

After executing the above command, the process will install the necessary WVD PowerShell modules on your computer. Once the PowerShell modules have been installed, run Import-Module Microsoft.RDInfra.RDPowershell to import the WVD PowerShell modules in the current PowerShell session.

Connecting to WVD

Once the PowerShell modules have been installed, connect to the WVD tenant by using the below command. The command below adds an RDS account to the current PowerShell session.

Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com"

To confirm whether you are connected to the WVD tenant or not, execute the Get-RDSContext command. This command will return the WVD tenant group to which you are connected as shown in the screenshot below:

Azure Windows Virtual Desktop

Exploring PowerShell cmdlets available with WVD PowerShell modules

There are several PowerShell cmdlets available with Microsoft.RDInfra.RDPowerShell PowerShell Modules. If you need to see a list of PowerShell cmdlets available with WVD PowerShell modules, please execute this command:

Get-Command *RDS* -Module Microsoft.RDInfra.RDPowerShell

The above PowerShell command lists all the PowerShell cmdlets available in Microsoft.RDInfra.RDPowerShell module as shown in the screenshot below:

Tenant name, host pool, and WVD PowerShell commands

Note that all the WVD PowerShell cmdlets require you to specify the WVD tenant name against which you are executing the PowerShell command and some of the WVD PowerShell cmdlets will require you to specify both WVD tenant and WVD host pool. You must know the WVD tenant name to be specified during the command execution. To get the WVD tenant name, you can execute the Get-RDSTenan cmdlet. For example, executing Get-RDSTenant for my test WVD tenant gives me the following information:

Azure Windows Virtual Desktop

Similarly, to list the number of host pools deployed in a WVD tenant, execute below PowerShell command:

Get-RDSHostPool -Tenant “TechGenix”

Once you have the WVD tenant name and host pools, you can utilize the PowerShell commands explained in the later section of this article to get the information you need.

Using WVD PowerShell modules

You can retrieve a wealth of information about the WVD environment using “Get-{cmdlet name}” PowerShell cmdlet. For example, if you need to get a list of host pools hosted on the current WVD Tenant, execute the following command:

Get-RDSHostPool -Tenant “TechGenix”

Similarly, if you need to retrieve the current RDS owner list from WVD tenant, execute the Get-RDSRoleAssignment PowerShell cmdlet.

Collecting user sessions

In case you need to collect the list of users connected and disconnected from a WVD tenant, you can use Get-RDSUserSession PowerShell cmdlet. Please note that you need to specify both WVD tenant name and host pool name in the command as shown below:

Get-RDSUserSession -TenantName “TechGenix” -HostPoolName “TechGenixPool”

When you execute the command, it connects to the specified WVD tenant and host pool and then provides you a list of active users. The command also lists the users who are disconnected. Normally, you would like to export this list to a CSV file. Adding Export-CSV cmdlet to the command will help you export the list to a CSV File as shown in the example command below:

Get-RDSUserSession -TenantName “TechGenix” -HostPoolName “TechGenixPool” | Export-CSV C:\Temp\AllWVDUsers.CSV

Collecting session hosts deployed in WVD

You can also retrieve the list of session hosts deployed in a WVD host pool. To retrieve the list of session hosts deployed in a WVD host pool, run this command:

Get-RDSSessionHost -TenantName “TechGenix” -HostPoolName “TechGenixPool”

And to export the list to a CSV file, execute this PowerShell command:

Get-RDSSessionHost -TenantName “TechGenix” -HostPoolName “TechGenixPool” | Export-CSV C:\Temp\WVDSessionHost.CSV

There are other WVD PowerShell commands that you can utilize to perform daily WVD operational tasks such as logging off users, disconnecting stale user sessions, and so on, but managing a large WVD environment using PowerShell alone is a difficult task. For example, it will take some time for you to manage all aspects of WVD, which includes checking active users, disconnected users, session hosts health status, file server health status, checking how many users are active in each hour, and so on. Wintellisys WVD Manager, as shown in the screenshot below, is a tool designed to perform several WVD-related admin tasks from within the tool.

The tool allows WVD admins to perform daily operational tasks such as adding and removing users from the WVD pool, checking how many users are active in each hour, allowing admins to disconnect stale sessions, and lets you know how many session hosts need to be active in each hour to reduce the infrastructure cost.

Logging off users

In case you need to log off users from WVD, you are required to use the Invoke-RdsUserSessionLogoff PowerShell cmdlet. Note that you are required to provide the following details with Invoke-RdsUserSessionLogOff cmdlet before you can log off a user:

  • Tenant name
  • Host n
  • User session ID
  • Session host Name

For example, this PowerShell command logs off a user from Host1.TechGenix.com whose session ID is 11.

Invoke-RdsUserSessionLogoff -TenantName "TechGenix" -HostPoolName "TechGenixPool" -SessionHostName "Host1.TechGenix.com" -SessionId 11

Unfortunately, you cannot provide user principal name with Invoke-RdsUserSessionLogOff cmdlet to log off a user. To get session ID and session host name for a user, you can use the Get-RDSUserSession PowerShell cmdlet. This cmdlet will help you get all users connected to WVD tenant with their session host name, session ID, and user principal name. If you are using WVD Manager, it provides you a User Sessions Manager console. The User Sessions Manager provides session ID, session host name, user principal name, and actions such as logging off/disconnecting a single user or bulk users with a single click.

Azure Windows Virtual Desktop: Now you know

In this article, we explained some of the useful PowerShell commands that Azure Windows Virtual Desktop admins can utilize to perform daily operational tasks. As we explained, most of the WVD PowerShell cmdlets require you to specify both tenant name and a host pool name. We also explained how you can disconnect a user from WVD using the Invoke-RdsUserSessionLogoff PowerShell cmdlet.

Featured image: Designed by Fullvector / Freepik

Leave a Comment

Your email address will not be published.

Scroll to Top