PowerShell ISE Can’t Find .NET Framework Class Methods
Posted by Chris on January 9, 2011
So I have started a new love affair with PowerShell and have started developing more and more things using it as it means I don’t have to bother with the UI, compiling or deployment – which for SOME situations is perfect. But today I came across an interesting problem while trying to load some .NET Framework classes.
I was attempting to call a method on the .NET Framework object I instantiated and found that the Dispose method that was mentioned on MSDN was being reported by PowerShell ISE as “Method invocation failed because [System.Security.Cryptography.RSACryptoServiceProvider] doesn’t contain a method named ‘Dispose’”.
After a bit of scratching my head I decided to look back at version v1.1 of the .NET Framework documentation to see if the Dispose method was present and guess what… it wasn’t. I went through the versions and found that it hadn’t been added till v3.5 of the .NET Framework. So after doing a few quick searches on Google I managed to find out on this post that by default the PowerShell ISE is loading an older version of the framework (presumably v2.0/v3.0) and that there are two ways to fix this.
- Using the following lines, edit the registry so that the .NET Framework loaded systemwide will be .NET Framework 4.0. WARNING: Use with Caution. See here for why
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
- Adding the following configuration to the ‘$psHome\powershell_ise.exe.config’ file. This will load the .NET Framework 4.0 for only the PowerShell ISE.
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0.20826" /> </startup> </configuration>
Either method will work but personally I just use the registry modification as it saves me the hassle with any future applications.



Craig Humphrey said
Hey Chris,
handy post, as I hit something similar, though with .Net 4.0.
Couple of things:
1. By setting those reg keys, all apps now run, by default, against .Net 4.0, which breaks many apps.
2. I narrowed it down by using a similar .exe.config for the powershell executable, which means that other apps don’t have a problem.
However, my new problem is that of course the SharePoint2010 module for Powershell wont load if you’ve set powershell to run under .Net 4.0!!!
So now I’m stuck! I’ve got a script that needs to use .Net 4.0 and SharePoint. Help!
Thanks
Craig
Chris said
Hi Craig,
You could still load the Microsoft.SharePoint.SPSite object by first importing the assembly with [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) and then creating the SPSite object with $spSiteObject = New-Object Microsoft.SharePoint.SPSite “http://spsite”.
Hope that helps.
-Chris