Skip to main content

Editing/Setting Mime Types with Powershell? [Resolved]

So I was wondering if there is a specific way to change/edit a mime type in IIS 7.5 using powershell?

So far I have come across solutions to add mimetypes such as: http://sclarson.blogspot.co.at/2011/08/adding-mime-type-to-iis-vis-powershell.html http://forums.iis.net/t/1192371.aspx and a couple of others that have books being referenced but those books do not contain any information on that, i checked.

It seems like using the Add-WebConfigurationProperty is used, but when i use this method for an existing mimetype then i will receive an error that is related to a duplicate item entry (since its already in there), so this method is out of the window.

I attempted to use the Set-WebConfigurationProperty instead but that resulted in it deleting ALL the existing mimetype entries and just leaving one of it.

So does anyone have a suggestion or perhaps knows the correct method of doing this? I also considered doing something like first deleting the entry and then adding it, the only problem is you will need to know both the mimetype fileExtension and the mimeTypes (i.e text/XML etc). So I assume I would need to first get both properties using the getwebconfigproperty method, then delete it by parsing the values from it into the delete-webpropconfig function and then add it... But going three steps just to set a mimetype seems excessive..

From what I understand, the appcmd.exe set config "values and params here" method will pretty much just straight-out do it.. the issue with this however is that unfortunately we cannot use it. And yes I understand I can make the powershell execute the appcmd command unfortunately this is not a viable option. I have tried googling and reading in books for solutions but have not come up with an answer so far on why

1) the set-webconfigurationproperty method deletes all other mimetype entries

2) the add-webconfigurationproperty will not allow me to perform it and throws an "error duplication" message

3) a working solution to do this in 1 line with powershell.


Question Credit: Orbital
Question Reference
Asked March 13, 2019
Posted Under: Network
9 views
4 Answers

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


credit: Orbital
Answered March 13, 2019

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.


credit: Castrohenge
Answered March 13, 2019

If you can't use appcmd, and set-webconfigurationproprty isn't working, maybe a custom built function will work?

Grab the COM DLL "Interop.IISOle.dll" and put it somewhere easily referenced (eg. reference the COM component "Active DS IIS Namespace Provider" in a dummy project, build and grab the DLL from the bin folder)

function AddMimeType ([string] $websiteId, [string] $extension, [string] $application)
{
    [Reflection.Assembly]::LoadFile("yourpath\Interop.IISOle.dll") | Out-Null;
    $directoryEntry = New-Object System
                      .DirectoryServices
                      .DirectoryEntry("IIS://localhost/W3SVC/$websiteId/root");
    try {
        $mimeMap = $directoryEntry.Properties["MimeMap"]
        $mimeType = New-Object "IISOle.MimeMapClass";
        $mimeType.Extension = $extension
        $mimeType.MimeType = $application
        $mimeMap.Add($mimeType)
        $directoryEntry.CommitChanges()
        } 
    finally {
        if ($directoryEntry -ne $null) {
            if ($directoryEntry.psbase -eq $null) {
                $directoryEntry.Dispose()
            } else {
                $directoryEntry.psbase.Dispose()
            }
        }
    }
}

AddMimeType "123456" ".pdf" "application/pdf"

Reference: The accepted answer to the the question How to use PowerShell to set Mime Type in an IIS website?


credit: Community
Answered March 13, 2019

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"}

credit: HiTech
Answered March 13, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA