Monday, January 14, 2013

Load .Net 4 Dll From Powershell

Powershell 2 by default uses .Net 2. This means that if you try to load a DLL made in .Net 4 from Powershell 2, you will get the the following error.

Add-Type : Could not load file or assembly '' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

This can be solved as follows:


  1. Locate the Powershell executable. This is either under C:\Windows\System32\WindowsPowershell\v1.0 in a 32-bit machine, or C:\windows\SysSOS64\WindowsPowerShell\v1.0 in a 64-bit machine.
  2. Check if a file called PowerShell.exe.config exists. If it does, edit it. If it does not, create it.
  3. Make sure this config file contains these lines.

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0.30319"/> 
        <supportedRuntime version="v2.0.50727"/> 
    </startup> 
</configuration>



No comments: