The easy way to run Powershell 2.0 using .NET Framework 4.0
Wednesday, May 16, 2012 at 2:02PM If you've been exploring creating your own modules and cmdlets with Visual Studio, you've likely stumbled onto the following problem: you are unable to load your module using the import-module command because of an error stating that “This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.”

The reason is simple enough. When PowerShell 2.0 starts it is using the old .NET Framework's CLR (which begins which is version 2.) so it will be able to load it modules compiled against .Net Framework 3.5 (which continues to use the version 2 CLR) but not .Net Framework 4.0 (which uses the version 3.0 CLR). This is perhaps a little surprising because Windows 7 comes with .NET Framework 4.0 installed.
You can see exactly what versions Powershell is using by examining the value of $PSVersionTable

Notice the value or CLRVersion begins with a "2".
In any case, this is a common occurrence with a straightforward solution. You can force PowerShell and PowerShell ISE to start using the .NET Framework 4.0 as documented in this StackOverflow question. The procedure involves creating two small XML files and placing them in the appropriate place.
Being lazy, of course, we hate manual steps, so here is a small PowerShell script that will automatically create and place the necessary files. Be aware that the script as shown below will overwrite existing .config files.
$config_text = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
"@$config_text| Out-File $pshome\powershell.exe.config
$config_text| Out-File $pshome\powershell_ise.exe.config
Start PowerShell as an Administrator an then run the script.
Now restart PowerShell and examine the value of $PSVersionTable.

Notice the value or CLRVersion now begins with a "4".
And now your .NET 4.0 module will load correctly.

saveenr |
8 Comments |
Reader Comments (8)
Thank you!
I came across this issue while trying to import the Veeam Backup 6.5 .dll module.
your script did the trick!
The correct way to do this (the MS recommended way):
Set Environment Variable 'COMPLUS_ApplicationMigrationRuntimeActivationConfigPath' to point to a path we will call P, this is much much cleaner than placing files in a system directory (FYI, the $pshome variable points to the wrong folder on Windows 64bit, the file should have gone to 'C:\Windows\SysWOW64\WindowsPowerShell', not System32 as this will give errors.)
Create your 'powershell.exe.activation_config' in this path P
from: http://msdn.microsoft.com/en-us/library/ff361644(v=vs.100).aspx
I've ran this script as Administrator but still receiving this error:
Access to the path 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe.config' is denied.
running on Windows 8
@Sergiu - Thanks for informing me. I'll look into it - but I am curious why you you need to do this on Windows 8 - PowerShell in Windows is already using the latest .NET Framework.
One question: When you have an elevated command prompt - either CMD.EXE or PowerShell - are you able to copy a file into that folder with the copy command?
Hi ,
i have come accross same error,i have followed the same steps.Now error has been changed.Can you please help me on this error.
Import-Module : Cannot load Windows PowerShell snap-in C:\Program Files (x86)\xRM CI Framework\CRM 2011\PowerShell Cmdl
ets\Xrm.Framework.CI.PowerShell.dll because of the following error: Unable to load one or more of the requested types.
Retrieve the LoaderExceptions property for more information.
Loader Exceptions:
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
At line:1 char:14
+ Import-Module <<<< "C:\Program Files (x86)\xRM CI Framework\CRM 2011\PowerShell Cmdlets\Xrm.Framework.CI.PowerShell.
dll"
+ CategoryInfo : ResourceUnavailable: (:) [Import-Module], PSSnapInException
+ FullyQualifiedErrorId : PSSnapInLoadFailure,Microsoft.PowerShell.Commands.ImportModuleCommand
I am also getting same error , could you please suggest ??
Import-Module : Cannot load Windows PowerShell snap-in C:\Program Files (x86)\Xrm CI Framework\CRM 2011\PowerShell Cmdl
ets\Xrm.Framework.CI.PowerShell.dll because of the following error: Unable to load one or more of the requested types.
Retrieve the LoaderExceptions property for more information.
Loader Exceptions:
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf385
6ad364e35' or one of its dependencies. The system cannot find the file specified.
At line:1 char:14
+ Import-Module <<<< "C:\Program Files (x86)\Xrm CI Framework\CRM 2011\PowerShell Cmdlets\Xrm.Framework.CI.PowerShell.
dll"
+ CategoryInfo : ResourceUnavailable: (:) [Import-Module], PSSnapInException
+ FullyQualifiedErrorId : PSSnapInLoadFailure,Microsoft.PowerShell.Commands.ImportModuleCommand
Very good post ! Very clear
Worked for me exactly as expected
Many thanks !!
I am also getting same error for Windows 2008 servers with Microsoft Power shell version 2.0(default power shell version).
could you please suggest how to fix this issue?