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.