Hello.
Thank-you for your responses. As requested the text of the script is the following:
<#
.SYNOPSIS
Gets the number of active user sessions on a remote computer.
.DESCRIPTION
Uses the built-in 'quser' command to query remote sessions.
Filters only sessions with the "Active" state.
Includes error handling for offline or inaccessible computers.
.PARAMETER ComputerName
The remote computer name or IP address.
.EXAMPLE
.\Get-ActiveUserCount.ps1 -ComputerName "Server01"
#>
param (
[Parameter(Mandatory = $true)]
[string]$ComputerName
)
try {
# Run quser remotely and capture output
$output = quser /server:$ComputerName 2>&1
# Check if the command failed
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to query $ComputerName. Details: $output"
exit 1
}
# Filter lines containing 'Active' (case-insensitive)
$activeUsers = $output | Where-Object { $_ -match '\sActive\s' }
# Count active sessions
$count = $activeUsers.Count
Write-Output "Computer: $ComputerName"
Write-Output "Active user sessions: $count"
}
catch {
Write-Error "Error querying ${ComputerName}: $_"
}