Let me start by saying that I am as excited as the next person to try new technology and upgrade all my software. Unfortunately, we cannot always upgrade the hardware/software in our production environments due to a variety of factors.
If you are able to upgrade your version of PowerShell to the latest and greatest, then please upgrade to the latest and greatest version (PowerShell 5 preview, anyone?). This blog post is for a (hopefully) small percentage of people who may find themselves stuck with an older version of PowerShell while needing to leverage something in a newer version of .NET Framework.
Running .NET 4 with PowerShell 2.0
There are two ways to tell PowerShell version 2.0 to utilize .NET Framework 4.
You can modify for all .NET applications or just for PowerShell itself. I cannot think of many situations where I’d want to force this setting globally for all .NET applications, so we’re going to address it just for PowerShell via some configuration files. All we need to do is create those configuration files and place them into their appropriate locations.
You’ll want to create powershell.exe.config and powershell_ise.exe.config and place them in your $pshome directory.
Here is what you want in both of those .config files:
Here is the location of $pshome by default:
- 32-bit machines: C:\Windows\System32\WindowsPowershell\v1.0
- 32-bit version: C:\Windows\SysWOW64\WindowsPowershell\v1.0
- 64-vit version: C:\Windows\System32\WindowsPowershell\v1.0
The quick and easy way to overwrite those files and place them where they need to be would be to run this as a script.
We place the entire content of the xml file into a variable via what’s known as a here-string. They let us use multi-line strings very easily. We output the new variable directly to two new files (overwriting existing files, so be careful).
Voila! Like magic, the new files are created. Simply restart your PowerShell console and you should be able verify which .NET CLR Powershell is using by using $PSVersionTable. We’re looking for the CLRVersion value, which is 4.0.30319.18444 in my case. That tells me I’m using .NET 4.
Hopefully you never find yourself in a position where you’re limited with your PowerShell versions. But, if you ever do find yourself in such a pickle, hopefully this can point you in the right direction.