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

I have the following Powershell script which uses WMI to update the HTTP custom headers on a website in IIS 6.

The script seems to work fine, no errors generated, and the resultant $website object correctly shows the updated HttpCustomHeaders value. When I look at that site in IIS, though, the values have not been updated. This is also verified by visiting the site and looking at the headers in Firebug - the values are not saved.

Any ideas what I'm doing wrong?

$website = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" -ComputerName $computerName -filter "ServerComment = '$websiteName'" -Authentication 6

$headers = $webSite.HttpCustomHeaders
$headers[0].Keyname = 'P3P: policyref="", CP="IDC DSP COR CUR DEV PSA IVA IVD CONo HIS TELo OUR DEL UNRo BUS UNI"'
$headers[0].Value = $null
$website.HttpCustomHeaders = $headers

I'm more than open to an alternative script that provides a better way of setting this value as above assumes there is already at least one header present.

I've also tried Set-WmiInstance -InputObject $website instead of $website.Put() but that made no difference.

share|improve this question
up vote 2 down vote accepted

Your code example saves the value to the metabase but it saves to the root object (w3svc/1) instead of the root vdir (w3svc/1/root).

Here's what will work in place of what you have:

$website = Get-WmiObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" -ComputerName $computerName -filter "ServerComment = '$websiteName'" -Authentication 6

$path = $ + '/root'
$vdir = [wmi]"root\MicrosoftIISv2:IIsWebVirtualDirSetting='$path'"

$headers = $vdir.HttpCustomHeaders
$headers[0].Keyname = 'P3P: policyref="", CP="IDC DSP COR CUR DEV PSA IVA IVD CONo HIS TELo OUR DEL UNRo BUS UNI"'
$headers[0].Value = $null
$vdir.HttpCustomHeaders = $headers

However, one more thing to watch for is that that replaces the first value rather than adding a new value. I'm out of time today to figure out how to do with with my limited PowerShell expertise, but it's going to be something along the lines of:

$bindingClass = [wmiclass]'root\MicrosoftIISv2:HttpCustomHeader'
$newheader = $bindingClass.CreateInstance()
$newheader.KeyName = "New Value"
$newheader.Value = $null

$headers += $header

There's a casting error so it's not quite right, but hopefully that points you in the right direction.

share|improve this answer
Brilliant - thanks Scott that works. Adding headers doesn't work as you've specified - let me know if you come up with something, I'll keep working on it too. – Duffman May 5 '11 at 10:01

Try outputting the current value first while you're testing. Then you can know that you properly bound to the site that you expected. For example, for the line after $headers=, do a Write-Host $headers[0].Keyname. Or do a Write-Host $webSite.ServerComment.

Basically, narrow it down to make sure that you have a handle on the correct website.

Also, I don't now PowerShell as well as I should at this point, but can you edit [0] and have it 'add' an entry? I bet that you need to do something else to actually add a value to an array.

share|improve this answer
Hi Scott - I've outputted values and ensured I'm on the correct site. Also verified output values after the fact and they state it has changed, however it hasn't. Looked all over for ways of adding HttpCustomHeaders but haven't found a way with ADSI or WMI in PowerShell. – Duffman May 4 '11 at 19:17
I found the answer after working on it for a while. I'll reply as a new answer so I can paste the code example. – Scott Forsyth - MVP May 4 '11 at 22:19

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.