Below is a quick-start cheat-sheet (with some context) for pulling a list of currently active Horizon sessions by user with PowerCLI.
1. Prerequisites
| Requirement | Notes |
|---|
| PowerShell 5.1+ (or PowerShell 7) | Windows 10/11 ships with 5.1; PS 7 is fine too. |
| VMware PowerCLI (v12.7+ recommended) | Install-Module VMware.PowerCLI (admin). |
Horizon PowerCLI module (VMware.VimAutomation.HorizonView) | Installs automatically with PowerCLI 12.7+. |
| Horizon admin creds (or an AD acct with the “Inventory Administrator” role) | Needed to connect to the Connection Server. |
2. Load / Update the Modules
# Update PowerCLI (optional but recommended)
Update-Module VMware.PowerCLI
# Make sure Horizon cmdlets are available
Get-Command -Module VMware.VimAutomation.HorizonView -Name *HVSession* | Select-Object -First 5
If nothing is returned, explicitly import/install:
Install-Module VMware.VimAutomation.HorizonView # downloads only the Horizon bits
3. Connect to the Horizon Connection Server
# Interactive prompt (simplest)
Connect-HVServer -Server "conn01.lab.local"
# …or provide credentials in-line (less secure!)
$cred = Get-Credential # popup
Connect-HVServer -Server "conn01.lab.local" -Credential $cred
# Optional: verify you’re connected
Get-HVGlobalSetting | Select-Object -First 1
4. List All Sessions, Then Filter “Active / Connected”
# Pull every session object into a variable
$sessions = Get-HVSession
# Horizon states of interest: CONNECTED, DISCONNECTED, LOGGED_OFF, ERROR, etc.
$activeSessions = $sessions | Where-Object { $_.State -eq "CONNECTED" }
# Quick display
$activeSessions | ft UserName, UserDisplayName, DesktopPoolName, MachineOrDisplayName, StartTime, ClientName -AutoSize
Example output:
UserName UserDisplayName DesktopPoolName MachineOrDisplayName StartTime ClientName
-------- --------------- --------------- -------------------- --------- ----------
CORP\jdavis Jason Davis HR-Pool HR-VDI-23 5/30/2024 9:17:04 AM VMWare-Horizon-2306
5. Common Enhancements
-
Show only unique users (some log into several desktops):
$activeSessions |
Sort-Object UserName -Unique |
Select-Object UserName, @{n='Desktops';e={($_ | Measure-Object).Count}}
-
Export for help desk / reporting:
$activeSessions |
Select-Object UserName, UserDisplayName, DesktopPoolName, MachineOrDisplayName,
StartTime, ClientIPAddress, ClientName, Protocol |
Export-Csv "ActiveHorizonSessions_$(Get-Date -f yyyyMMdd_HHmm).csv" -NoTypeInformation
-
Include remote machine details (vCenter):
$activeSessions |
Select-Object UserName,
@{n='DesktopVM';e={$_.MachineOrDisplayName}},
@{n='vCenterVM';e={(Get-VM -Name $_.MachineOrDisplayName).Name}},
@{n='PowerState';e={(Get-VM -Name $_.MachineOrDisplayName).PowerState}}
6. Quick Function Wrapper
Paste this into your profile or a module to reuse:
function Get-HVActiveUser {
param(
[string]$Server = "conn01.lab.local",
[pscredential]$Credential = (Get-Credential)
)
Connect-HVServer -Server $Server -Credential $Credential -WarningAction SilentlyContinue
Get-HVSession |
Where-Object { $_.State -eq "CONNECTED" } |
Select-Object UserName, UserDisplayName, DesktopPoolName,
MachineOrDisplayName, StartTime,
ClientName, ClientIPAddress, Protocol
}
Usage:
Get-HVActiveUser | Out-GridView
7. Troubleshooting Tips
• “Get-HVSession : The term is not recognized” → The HorizonView module isn’t loaded/installed.
• “Failed to connect to server” → Check firewall (TCP 443) and verify the Connection Server’s certificate chain.
• Seeing empty results? Make sure you’re hitting the broker that owns the sessions (or connect to your load-balancer VIP).
8. REST Alternative (Horizon 8 2206+)
For environments where the REST API is enabled, you can do:
$hdrs = @{ "Authorization" = "Bearer $token" }
Invoke-RestMethod -Method Get `
-Uri "https://conn01.lab.local/rest/session" `
-Headers $hdrs |
Where-Object state -eq "CONNECTED" |
Select-Object user_name, desktop_id, client_name
…but for most admins the PowerCLI cmdlets above are simpler.
Happy scripting! 🖥️💙