Editing/Setting Mime Types with Powershell?
Solution 1:
The only thing that seemed to work is this:
& $Env:WinDir\system32\inetsrv\appcmd.exe set config /section:staticContent /-"[fileExtension='.eml']"
& $Env:WinDir\system32\inetsrv\appcmd.exe set config /section:staticContent /+"[fileExtension='.eml',mimeType='application/octet-stream']"
So i have gone with this method, as perhaps mentioned earlier any kind of set-webconfiguration command has simply overwritten the entire mimetype list within the IIS mimetype menu.
Hope this helps someone in the future
Solution 2:
You can use Set-WebConfigurationProperty
but you have to specify in the XPath filter which mimetype you want to change.
For example the following command will change the mime type with the file extension ".currentExt" to ".changedExt" in the root web.config used by the IIS server:
Set-WebConfigurationProperty -Filter "//staticContent/mimeMap[@fileExtension='.currentExt']" -PSPath IIS:\ -Name fileExtension -Value ".changedExt"
mimeMap
is used in the XPath because the structure of the application.host.config is as follows:
<staticContent lockAttributes="isDocFooterFileName">
<mimeMap fileExtension=".323" mimeType="text/h323" />
<mimeMap fileExtension=".aaf" mimeType="application/octet-stream" />
<mimeMap fileExtension=".aca" mimeType="application/octet-stream" />
...
</staticContent>
I'm assuming you were modifying the code suggested by http://sclarson.blogspot.co.at/2011/08/adding-mime-type-to-iis-vis-powershell.html when trying to modify the mimeTypes using Set-WebConfigurationProperty
. The reason all mimeTypes were being overwritten when using the following modification on the code in the link
set-webconfigurationproperty //staticContent -name collection -value @{fileExtension='.otf'; mimeType='application/octet-stream'}
is because the entire collection of mimeTypes is being replaced by just the one entry.
Solution 3:
I understand this question is old and has been answered but I would like to add to the conversation for new viewers. Tested on IIS 8.5 Windows Server 2012R2
. Unfortunately, I don't have access to IIS 7.5.
Using .zip extension as my example file extension
Adding a new MIME type:
Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/staticContent" -Name "." -Value @{ fileExtension='.zip'; mimeType='application/x-zip-compressed' }
Editing an existing MIME type:
Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/staticContent/mimeMap[@fileExtension='.zip']" -Name "fileExtension" -Value ".example"
Removing an existing MIME type:
Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/staticContent" -Name "." -AtElement @{ fileExtension= '.zip' }
Reading/Getting an existing MIME type:
$getSetting = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/staticContent" -Name "."
$getSetting.Collection | Where-Object { $_.fileExtension -eq ".zip"}
#OR
Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/staticContent" -Name "." | Where-Object { $_.fileExtension -eq ".zip"}