Powershell – Getting all virtual directories for a IIS6 web site using WMI

iis-6powershellwmi

I want to list all virtual directories that belong to a web site with a certain name using WMI and PowerShell.

I know I can list all virtual directories on a server using the code below, but how can I filter out only those that belong to a specific site?

Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2"

Best Answer

Well, taking the simplest approach here and filtering based on the properties returned from your given example, I would probably opt to use the site identifier portion in the Name property:

Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2" | `
     Where-Object { $_.name -like "W3SVC/1/*" }

The above example only shows virtual directories on the default website that is set up with IIS first install. This always has the identifier 1.

Note: the backtick ` after the bar is the line continuation character (actually it's the escape character, but I'm escaping the EOL,) like _ in Visual Basic. I'm using this so the ugly horizontal scrollbars don't show up in the code block above.

-Oisin