Setting IIS Mime Type via script

If you need to add (or edit) a mime type entry stored in IIS configuration files, you have a few tools that you can use.

First off, and this is by far the easiest way, you can leverage the IIS Manager.

  • Open inetmgr.exe
  • Select the “scope”
  • Select the Mime Types feature
  • Edit an existing entry or add a new one by clicking the “Add..” action in the right pane

Of course, if you need to automate these settings, the graphical UI is not the tool of your choice.

You could, instead, use scripting, either leveraging the appcmd console application that is part of the IIS management tools, or writing a couple of lines of code (PowerShell, why not?).

The approach I prefer is using appcmd, which is not so complicated indeed.

For example, here I’m registerint the video/mp4 mime type for the .mp4 extemsion (type the following command in a single line, here you may find line breaks for reading convenience):

appcmd.exe set config “Default Web Site” -section:system.webServer/staticContent /+”[fileExtension=’mp4′,mimeType=video/mp4]”

Managing IIS settings through PowerShell

Do you miss the IISAPP command line tool that was available with IIS6?

Do you feel that APPCMD is not enough?

Enter the WebAdministration PowerShell module!

Its usage is straightforward, as it it leverages the concept of providers and drives that the PowerShell engine uses to unify the way you access resources on your server. And it provides several commandlets that can help you automate most (if not all) of the web administration tasks.

In order to use the WebAdministration module, you need to launch PowerShell with elevated rights, then just import the module:

Import-Module WebAdministration

A quick Get-Command will show you that there’s something to try!

gcm -Module WebAdministration | Measure-Object

In my environment, this shows a total count of 74 items (71 commandlets, 2 aliases and one function).

I’ll let you explore the list of commandlets yourself, while I spend some other words about the function, whose name is “IIS:”.

Just like C:, D: etc, which are indeed just functions that set the current location to the root of the C or D drives, the IIS: function uses the Set-Location commandlet to “move” to the IIS: drive.

Which, of course, is not a drive based on the FileSystem provider, but is relies on the WebAdministration PowerShell provider that you get inside the WebAdministration module.

Thanks to this approach, you can easily “navigate” within IIS settings just by using “dir” or “cd” (which, btw, are aliases too). For examples, if you need to list the IIS Web Sites on the local server, just try with this syntax:

dir IIS:\Sites

Or if you need to get, say, the list of running w3wp.exe processes for a specific application pool, you can type something like this:

dir IIS:\AppPools\<NameOfTheAppPool>\WorkerProcesses

Nice, isn’t it?