Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I iterate through all the websites hosted on an IIS 6.0 (Windows Server 2003) using powershell?

Is there a way to do it using WmiObject ?


share|improve this question
Are you simply looking for a list of their names or do you expect some sort of site object to be passed back to be acted upon? – pk. Apr 10 '12 at 18:46
Not just listing the sites, but be able to iterate through each website and add new HttpCustomHeaders to it in IIS6.0. – user117048 Apr 11 '12 at 17:48

List them using ADSI or WMI as follows --

$iis = [ADSI]"IIS://localhost/W3SVC" 
$iis.psbase.children | where { $_.schemaClassName -eq "IIsWebServer"} | select ServerComment

Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" | Select ServerComment

To add a new HttpCustomHeader as you've mentioned in your comment, I think you can use the following technique. Please test thoroughly for I did not.

$wmiSiteArray = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2"

foreach ($site in $wmiSiteArray)
  $path = $ + '/root'
  $vdir = [wmi]"root\MicrosoftIISv2:IIsWebVirtualDirSetting='$path'"
  $bindingClass= [wmiclass]'root\MicrosoftIISv2:HttpCustomHeader'
  $newHeader = $bindingClass.CreateInstance()
  $newHeader.KeyName = "CustomHeader: BlahBlah"
  $newHeader.value = $null
  $vdir.HttpCustomHeaders += $newHeader.PSObject.BaseObject
share|improve this answer

This post:

has this solution:

Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2" | `
     Where-Object { $ -like "W3SVC/1/*" }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.