You are absolutely right to get that error, and my apologies for not including the prerequisite step in the initial script.
That error message is PowerShell's way of saying, "I don't know what command you're talking about."
The reason is that the cmdlets for VMware Horizon (Connect-HVServer, Get-HVSession, etc.) are in a separate PowerCLI module called VMware.VimAutomation.HorizonView that needs to be installed and then loaded (imported) into your session.
Here are the steps to fix this.
Step 1: Check if the Horizon Module is Installed
First, see if you already have the module on your system. Run this in PowerShell:
Get-Module -ListAvailable -Name VMware.VimAutomation.HorizonView
- If you see output listing the module, you have it installed and can skip to Step 2.
- If you see no output, you need to install it.
Step 2: Install the Horizon Module (If Needed)
If the module wasn't found, run this command in PowerShell. It's best to run PowerShell as an Administrator for this step.
Install-Module -Name VMware.VimAutomation.HorizonView
You may be asked to approve installing from the 'PSGallery' repository. This is normal and safe; type Y and press Enter.
Step 3: The Corrected and Complete Script
Now that the module is installed, you need to import it at the start of your script. This explicitly loads the Get-HVSession command into your session.
Here is the revised, fully-functional script.
# ===================================================================
# SCRIPT TO GET VMWARE HORIZON ACTIVE USER SESSIONS
# ===================================================================
# Step 1: Import the Horizon Module (This is the crucial missing step)
# This loads the necessary commands like Get-HVSession and Connect-HVServer
Write-Host "Importing VMware Horizon Module..."
try {
Import-Module -Name VMware.VimAutomation.HorizonView -ErrorAction Stop
}
catch {
Write-Host "Error: The VMware.VimAutomation.HorizonView module could not be loaded." -ForegroundColor Red
Write-Host "Please ensure it is installed by running: Install-Module -Name VMware.VimAutomation.HorizonView" -ForegroundColor Yellow
return # Exit the script if the module can't be loaded
}
# Step 2: Connect to the Horizon Connection Server
# --- Replace "connection-server.domain.com" with your actual server FQDN ---
$hvServer = "connection-server.domain.com"
Write-Host "Connecting to Horizon Server: $hvServer..."
$hvConnection = Connect-HVServer -Server $hvServer -Credential (Get-Credential)
if (-not $hvConnection) {
Write-Host "Failed to connect to the Horizon Server. Please check the server name and credentials." -ForegroundColor Red
return # Exit if connection fails
}
# Step 3: Get all active sessions
Write-Host "Fetching active sessions..."
$sessions = Get-HVSession -State CONNECTED
# Step 4: Display the results in a formatted table
Write-Host "Active User Sessions Found: $($sessions.Count)"
$sessions | Select-Object UserName, DNSName, State, DesktopId, SessionType, SessionProtocol | Format-Table -AutoSize
# Step 5: Disconnect from the Horizon server
Write-Host "Disconnecting from Horizon Server..."
Disconnect-HVServer -Server $hvServer -Confirm:$false
# ===================================================================
# OPTIONAL: Exporting the data to a CSV file
# Uncomment the lines below if you want to save the output to a file
# ===================================================================
# $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
# $filePath = "C:\temp\Horizon_Active_Sessions_$timestamp.csv"
# $sessions | Select-Object UserName, DNSName, State, DesktopId, SessionType, SessionProtocol | Export-Csv -Path $filePath -NoTypeInformation
# Write-Host "Session data exported to $filePath"
# ===================================================================
What to Do If It Still Fails
If you still encounter issues, check the following:
- PowerShell Execution Policy: You might not be allowed to run scripts. You can check your policy with
Get-ExecutionPolicy. To allow scripts for your current session only (a safe option), run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
- Run as Administrator: Close your PowerShell window and re-open it by right-clicking the PowerShell icon and selecting "Run as administrator". This often solves permission issues, especially with module loading.
Try running the corrected script. It should now recognize Get-HVSession and work as expected