When you try to add an assembly that you downloaded from a network share or the internet to your PowerShell session via ‘Add-Type -Path C:\folder\your-assembly-name.dll’ you might a receive an error like this:

PS > Add-Type -Path 'C:\folder\your-assembly-name.dll';
Add-Type : Could not load file or assembly 'file:///C:\folder\your-assembly-name.dll' or one of its
dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -Path 'C:\folder\your-assembly-name.dll'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

The result itself could give you a hint and querying the inter you will probably others experiencing the same error as described in Visual Studio Project Sample Loading Error: Assembly could not be loaded and will be ignored. Could not load file or assembly or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515), though in a different environment.

But the good thing is that you can examine the error further within PowerShell directly by just looking at the exception of the ErrorRecord itself, and especially at the InnerException which in this case will tell us about the actual error in detail:

PS > $error[0]
Add-Type : Could not load file or assembly 'file:///C:\folder\your-assembly-name.dll' or one of its
dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -Path 'C:\folder\your-assembly-name.dll'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

PS > $error[0].Exception
Could not load file or assembly 'file:///C:\folder\your-assembly-name.dll' or one of its
dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
PS > $error[0].Exception.InnerException
An attempt was made to load an assembly from a network location which would have caused the assembly 
to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not 
enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox 
the assembly, please enable the loadFromRemoteSources switch. 
See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

So we learn that the error is or might be related to the nature that we were trying to load a resource from a network (which qualifies for Internet resources as well) – so the simple solution is to just unblock the file (or its container before unpacking) via the Windows Explorer via ‘Properties, Security, Unblock’. After that the ‘Add-Type’ will simply succeed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.