PowerShell: Add Custom Header to IIS Website Responses

Here’s a PowerShell snippet that will add a custom header to the response from a designated website using the Add-WebConfigurationProperty cmdlet:

Import-Module WebAdministration
 
Add-WebConfigurationProperty //system.webServer/httpProtocol/customHeaders  "IIS:\sites\MySuperWebsite" -AtIndex 0 -Name collection -Value @{name='Access-Control-Allow-Origin';value='*'}

The Add-WebConfigurationProperty cmdlet is part of the IIS WebAdministration module.

Hope this helps!

This entry was posted in PowerShell. Bookmark the permalink.

2 Responses to PowerShell: Add Custom Header to IIS Website Responses

  1. R says:

    What is the purpose of:

    -AtIndex 0 -Name collection

    ?

  2. Greg says:

    Those parameters ensure the header is inserted at position 0 (the first header) of the specified headers within the configuration collection.

Leave a Reply

Your email address will not be published. Required fields are marked *