Looking for a quick way to see how many users are currently connected to each of your Client Access Servers? This might be useful if you want to get a rough idea on load distribution, or when you’re getting ready to drain stop a server in a CAS array prior to maintenance. It’s always good to know how many users might be affected by an taking a server down. The following PowerShell function will grab performance counter data from each server to determine the number of active OWA and RPC connections:

function Get-CASActiveUsers {
  [CmdletBinding()]
    param(
    [Parameter(Position=0, ParameterSetName="Value", Mandatory=$true)]
    [String[]]$ComputerName,
    [Parameter(Position=0, ParameterSetName="Pipeline", ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
    [String]$Name
  )

  process {
    switch($PsCmdlet.ParameterSetName) {
      "Value" {$servers = $ComputerName}
      "Pipeline" {$servers = $Name}
    }
    $servers | %{
      $RPC = Get-Counter "\MSExchange RpcClientAccess\User Count" -ComputerName $_
      $OWA = Get-Counter "\MSExchange OWA\Current Unique Users" -ComputerName $_
      New-Object PSObject -Property @{
        Server = $_
        "RPC Client Access" = $RPC.CounterSamples[0].CookedValue
        "Outlook Web App" = $OWA.CounterSamples[0].CookedValue
      }
    }
  }
}

Just add the function to your shell session and when you run it, specify one or more server names using the -ComputerName parameter:

Get-CASActiveUsers -ComputerName cas1,cas2

The function is also written to support pipeline input, so you can pipe the Get-ClientAccessServer cmdlet to it as well:

Get-ClientAccessServer | Get-CASActiveUsers

It can take a few minutes before the values are updated as clients connect or disconnect. For example, if you compare the values returned by the function before and after opening Outlook and OWA on a couple of machines, you may notice that it can take a few minutes before the counter values reflect the changes.