Exploring IIS with PowerShell

I need to write a Web Installer with a bit more complex logic than the standard setup provides, and came across pages like Creating Sites and Virtual Directories Using System.DirectoryServices that deal with .Net classes I have never seen before. (Note: while the page deals with IIS 6, the classes also work on IIS 7 if the IIS 6 Management Compatibility feature is installed)

Trying to figure out what System.DirectoryServices can accomplish, I chose PowerShell to poke around in IIS.

PowerShell needs to be run as administrator, otherwise you will get an “access denied” error (unless you provide login information in the DirectoryEntry constructor)

First, we load the DirectoryServices.dll:

> [system.reflection.assembly]::loadwithpartialname(

Next, we can retrieve the IIS service entry using a DirectoryEntry

> $service = new-object system.directoryservices.directoryentry(
> $service.SchemaClassName

See this chart for the IIS Admin Object Hierarchy.

The children of the IIsWebService are

> $service.Children | select-object  SchemaClassName, Path, AppRoot
SchemaClassName       Path                                    AppRoot
---------------       ----                                    -------
IIsFilters            IIS://
IIsApplicationPools   IIS://
IIsWebInfo            IIS://
IIsWebServer          IIS://                 {}

Let’s get the web server root

> $root = new-object system.directoryservices.directoryentry(
> $root.Children | select-object  SchemaClassName, Path, AppRoot

SchemaClassName   Path                                    AppRoot
---------------   ----                                    -------
IIsWebVirtualDir  {D:\MyVirtDir}                          {}
IIsWebDirectory   IIS://   {/LM/W3SVC/1/ROOT/MyWebApp}

An IIsWebDirectory has a defined AppRoot property, whereas an IIsWebVirtualDir has a file system directory as Path, but no AppRoot.

You can instantiate a DirectoryEntry for both IIsWebDirectory and IIsWebVirtualDir using the value of the ADsPath property:

> $root.Children | select-object  SchemaClassName,  ADsPath, Path
SchemaClassName   ADsPath                                  Path
---------------   -------                                  ----
IIsWebVirtualDir  IIS://   {D:\MyVirtDir}
IIsWebDirectory   IIS://    IIS://

2 Responses to Exploring IIS with PowerShell

  1. […] to create Virtual Directories using System.DirectoryServices.DirectoryEntry as described in my previous post. The installer class is called by the web installer using the Custom Actions […]

  2. […] to install on a Windows 2008 Server running IIS 7. The prerequisite knowledge is described in my previous […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: