Printing/Escaping a variable name in Powershell

In Powershell, if you include a variable name (like $PROFILE) in a statement that writes to the screen, it will be replaced with the contents of the variable:

PowerShell 7.4.1
PS C:\Users\Ryan> Write-Output "this is my profile: $PROFILE"
this is my profile: C:\Users\Ryan\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

But what if you want to actually out the name of your variable instead of the contents? Like if you’re displaying an example statement to execute? In that case, you can escape it by using the backtick character (“`” – the backwards single-quote on in the upper left of your keyboard on the same key as the “~” tilde). In that case, your result will look like this:

PowerShell 7.4.1
PS C:\Users\Ryan> Write-Output "this is my profile: `$PROFILE"
this is my profile: $PROFILE

In my case, I was trying to print some instructions to the screen that included the $PROFILE, so that was the workaround.

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"