Some SQL, some .NET, and whatever else


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"
Comments (10) Trackbacks (1)
  1. can you please tell me how to use this command?

  2. 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 = “”

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

    • 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!

  3. 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.


    • 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.

  4. Is this code really working in PS? where to run this on? is there any way run this command on shared hosting sites like on godaddy?

    • 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.

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

    • 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 comment

Why ask?