Share via

Problem running a PowerShell script

Kaplan, Andrew H 226 Reputation points
2025-12-22T14:19:30.05+00:00

Hello. I am trying to get a PowerShell to run that will list the current active user sessions on a remote computer running the Windows 11 Pro operating system. The name of the script is Get-ActiveUserCount.ps1, and the syntax 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: $_"

}

The command syntax that I used when running the script is the following:

.\Get-ActiveUserCount.ps1 <remote hostname>

or

.\Get-ActiveUserCount.ps1 <remote ip address>

The error message that comes up is the following:

At C:\Users<username>\Documents\WindowsPowerShell\Get-ActiveUserCount.ps1:42 char:33

  • Write-Error "Error querying $ComputerName: $_"
    
  •                             ~~~~~~~~~~~~~~
    

Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

Windows for business | Windows Server | Networking | Other
0 comments No comments

2 answers

Sort by: Most helpful
  1. Harry Phan 20,535 Reputation points Independent Advisor
    2025-12-23T01:43:42.3266667+00:00

    Hi Kaplan, the issue arises because in PowerShell, when you reference a variable inside a string that also contains a colon (:), the parser may misinterpret it. In your catch block, the line:

    Write-Error "Error querying $ComputerName: $_"

    is causing the parser error. To fix this, you can wrap the variable reference in curly braces to clearly delimit it, like this:

    Write-Error "Error querying ${ComputerName}: $_"

    This ensures PowerShell correctly interprets $ComputerName as a variable, followed by a colon and the error object. Alternatively, you can use string concatenation:

    Write-Error ("Error querying " + $ComputerName + ": " + $_)

    After making this change, your script should run without the parser error and properly display the error message when a query fails.

    I hope this helps you get your script working smoothly! If you find this answer helpful, please don’t forget to hit “Accept Answer” 🙂.

    Harry.

    Was this answer helpful?

    1 person found this answer helpful.

  2. AI answer

    2025-12-22T14:19:37.2866667+00:00

    The error message you are encountering indicates that there is an issue with how the variable $ComputerName is being referenced in your script. Specifically, the error occurs because PowerShell is interpreting the $_ variable incorrectly due to the context in which it is used. To fix this issue, you should use the ${} syntax to properly reference the variable within the string. Here’s how you can modify the line causing the error:

    Change this line:

    Write-Error "Error querying $ComputerName: $_"
    

    To this:

    Write-Error "Error querying $ComputerName: ${_}"
    

    This change will ensure that PowerShell correctly interprets the variable reference. After making this adjustment, try running your script again with the appropriate command syntax:

    .\Get-ActiveUserCount.ps1 -ComputerName "<remote hostname>"
    

    or

    .\Get-ActiveUserCount.ps1 -ComputerName "<remote ip address>"
    

    This should resolve the error and allow your script to execute as intended.

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.