Powershell command to get current sessions on an IIS site

After a Powershell session at SQL Saturday (Phoenix #131) this weekend, I’m now suddenly on the lookout for handy powershell commands. The first one lets you see the number of Active* sessions on your IIS site (* because HTTP is stateless, it’s really the number of connections that have been opened recently – not what’s currently active, which is likely next to zero. I tried browsing around on a test site and it showed only my one user connected).

To get the currently active user count, here’s the powershell:

# Ensure you use the server's actual name, not LOCALHOST, which won't work
$Servername = "Your Server Name"
$Sitename = "Name of your IIS Site"

Get-Counter "\\$ServerName\web service($SiteName)\current connections"

11 thoughts on “Powershell command to get current sessions on an IIS site”

    1. Here’s a technet article that walks you through how to run Powershell scripts:

      http://technet.microsoft.com/en-us/library/ee176949.aspx

      and some more examples here:

      http://www.windowsitpro.com/article/windows-powershell/running-powershell-scripts-is-as-easy-as-1-2-3-103427

      Essentially, you can think of Powershell as a hybrid between a command shell and a programming language – it allows you run simple commands, or you can write more complicated scripts for advanced tasks.

  1. I had to prefix the servername with an extra ‘\’ e.g.

    # Ensure you use the server’s actual name, not LOCALHOST, which won’t work
    $Servername = “myserver”
    $Sitename = “go.sitecheckerapp.com”

    Get-Counter “\\$ServerName\Web Service($SiteName)\Current Connections”

    1. Thanks – when I moved my blog recently, the export/import dropped all the backslashes from every one of my posts. I went through to add them all by hand, but I missed this one – I appreciate the catch!

  2. Hi,

    Are you sure this counter gives the session count? It is too dynamic on my server for session count. A session should last 20 minutes. Seems to be the current requests or something else.

    Martin

    1. It’s not really “active sessions” in the sense that you’re thinking that a timeout is 20 minutes. It’s also not active requests, as far as I can tell – a request is filled too quickly and the connection dropped, so there are often no open connections at all. It seems to be somewhere in between – a measurement of open recent connections that gives a general idea of the number of people browsing the application at any given time. From what I’ve seen, it counts the connection for a few minutes and then drops it, so it must assume that if you don’t request a new page within a minute or so, you’re gone.

    1. This code works for me on my Windows servers that have Powershell installed and I haven’t had any issues. Unfortunately, you need to be able to run it from a Powershell session on the server hosting IIS, which you won’t have access to in a shared hosting situation like GoDaddy. If you host your own website you’d be able to use it, but in cases like yours, you’d likely be much better off adding some basic Google Analytics code to your web pages and viewing total user volume that way so you can see the trends.

      Though it’s been handy a few other times too, I generally just use this snippet to ensure nobody is using an application server I manage before I reboot it after maintenance – it’s off hours so it’s generally quiet, but I just like to confirm and this is an easy way to do it.

  3. Hi, is there a way to get IIS session count for a time internal? lets say IIS session count for last 5 minutes??

    1. I’ve never found detail about exactly what this means or how to change the frequency. I doubt it’s actually “open connections at this very second”, as that would dramatically undershoot the number of website visitors you have (since HTTP is largely fetching files quickly and then waiting around for the user to do something, so there are often only a fraction of your users fetching something at the moment). If you do find a way to do this, please leave a note as I’d love to see it, but I’ve never found a way to alter the timeline here.

Leave a Reply

Your email address will not be published. Required fields are marked *