My little script works as expected in Powershell ISE:
$FilePath = "C:\users\Daniel\Desktop\test.pdf"
Add-Type -AssemblyName "System.Web"
$DocExtension = [System.IO.Path]::GetExtension($FilePath)
$DocMimeType = [System.Web.MimeMapping]::GetMimeMapping($DocExtension)
Write-Output "Extension: $DocExtension `nMIME-type: $DocMimeType"
Running it in Powershell.exe I get the following error: Unable to find type [System.Web.MimeMapping].
I googled and ChatGPTed for an hour but still cannot find any solution that would work for me.
I want to run that code block from above in a script that gets executed with Powershell, not ISE.
Help is much appreciated, thanks
I expect the script to run in Powershell.exe too.
The
System.Web.MimeMapping
type is only supported in the legacy .NET Framework that underlies the legacy Windows PowerShell edition, which is the only edition supported in the obsolescent ISE.[1]For instance, it doesn't know about the fairly common
application/json
(.json
) andtext/csv
(.csv
) media types.The modern .NET (Core) framework underlying the modern, cross-platform PowerShell (Core) 7+ edition doesn't have this type anymore.
The third-party
MimeTypes
NuGet package offers an alternative, but use from PowerShell is nontrivial, especially in Windows PowerShell; a PowerShell (Core)-only solution is feasible, however - see the bottom section. The next section shows a cross-edition solution, i.e. one that works in both Windows PowerShell and PowerShell (Core).Cross-PowerShell-edition solution:
The
MimeTypes
NuGet package is built on the Node.js mime-db project, which offers its list of media-type definitions in the form of an online JSON file that is updated periodically.That JSON file can be downloaded and parsed on demand:
The above yields
'text/plain'
.Important:
The JSON format may change in the future, so using a specific release of the JSON file is advisable.
The wrapper function below:
Get-MediaTypeByFilename
function wrapper:Assuming the function, whose source code is below, is defined, here's a sample call:
Cross-edition
Get-MediaTypeByFilename
source code:PowerShell (Core) 7+-only solution:
The
MimeTypes
NuGet package a source code-only package that requires compilation, which you can do on demand withAdd-Type
. However, because the source code uses newer C# features that aren't supported in Windows PowerShell'sAdd-Type
, the following function works in PowerShell (Core) 7+ only.Sample call:
Important:
The code below downloads the latest version of the
MimeTypes
source code directly from GitHub (viaInvoke-RestMethod
), directly from themaster
branch.https://raw.githubusercontent.com/khellang/MimeTypes/v2.4.1/src/MimeTypes/MimeTypes.cs.pp
Use of
Add-Type
for on-demand compilation incurs a once-per-session performance penalty.PowerShell (Core)-only
Get-MediaTypeByFilename
source code:[1] The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 7+. The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension.