3

I am trying to write a powershell script that i can use to add custom http headers in IIS instead of manually adding them. I did not have any luck finding anything on the web so far. Can someone please point me to right direction.

Thanks

3
  • Like this (the code part on the bottom)? Commented Aug 24, 2016 at 15:45
  • 1
    Add-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST ` -Name . -Filter system.webServer/httpProtocol/customHeaders ` -AtElement @{name = "X-Frames-Options" ; value='sameorigin'} Commented Aug 24, 2016 at 15:49
  • it adds only one but when i try to add multiple at the same time it does not work. Commented Aug 24, 2016 at 15:50

2 Answers 2

6

I was able to successfully add 2 or more headers using the following:

Add-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST `
    -Name . -Filter system.webServer/httpProtocol/customHeaders `
    -AtElement @{name = "X-Frames-Options" ; value = 'sameorigin' }

Add-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST `
    -Name . -Filter system.webServer/httpProtocol/customHeaders `
    -AtElement @{name = "Cache-Control" ; value = 'private, no-cache, no-store, max-age=0'}
Sign up to request clarification or add additional context in comments.

Comments

2

As an alternative, avoiding the cmdlets and going direct to appcmd still works:

PS> C:\Windows\System32\inetsrv\appcmd.exe set config  -section:system.webserver/httpProtocol /+"customHeaders.[name='myCustomName',value='myCustomValue']"

1 Comment

Not a powershell cmdlet, but useful to know that appcmd.exe still exists in case the cmdlets aren't on a particular system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.