Alex Rivera | Logout

PowerShell: Possible to Determine a File's MIME Type?

Asked 2012-07-28T05:21:20.657
10

Is it possible for PowerShell to determine what a given file's type is? For example, if I pass it a path of C:\Foo.zip, can I have it determine that the file at that path is, in fact, a zip file and not something else?

Edit
Report

1 Answer

4

You can get the mime types from the registry. Unfortunately the HKEY_CURRENT_USER hive isn't auto-mapped as a drive so you'll have to put a check in for that first.

function Get-MimeType()
{
  param($extension = $null);
  $mimeType = $null;
  if ( $null -ne $extension )
  {
    $drive = Get-PSDrive HKCR -ErrorAction SilentlyContinue;
    if ( $null -eq $drive )
    {
      $drive = New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
    }
    $mimeType = (Get-ItemProperty HKCR:$extension)."Content Type";
  }
  $mimeType;
}

Here's an example usage

PS> Get-MimeType -extension .zip
application/x-zip-compressed

-Joe

answered 2012-10-24T16:46:13.057

Your Answer