When Add-Type Fails…

by Jul 21, 2017

Add-Type can be used to load additional .NET assemblies from DLL files into PowerShell. This works well most of the time, and here is a sample call (that would require the SharePoint DLL to be available of course):

 
PS> Add-Type -Path "C:\SharepointCSMO\Microsoft.SharePoint.Client.dll"
 

With some DLL files, this fails though, and PowerShell returns an exception saying “Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”.

If this happens, a workaround is to use the deprecated (but still available) LoadFrom() method:

 
PS> [Reflection.Assembly]::LoadFrom("C:\SharepointCSMO\Microsoft.SharePoint.Client.dll") 
 

Why is Add-Type failing? Add-Type maintains a hand-tailored list of version numbers tied to assemblies. So when you load an assembly file that is older than the expected version, Add-Type refuses to load it. LoadFrom() in contrast does not care about versions, and works with older versions as well.

Twitter This Tip! ReTweet this Tip!