Enable .NET 4 Runtime for PowerShell and Other Applications

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to set all applications or just PowerShell 2.0 on a Windows 2008 R2 server to load the .NET 4 Runtime. PowerShell 2.0 uses .NET 2.0 by default. This can be useful for getting the added overload for accessing 64-bit registry keys on remote systems with custom WIM objects (Not invoking a command remotely).


Display Current Versions

$PSVersionTable

Example

Name                           Value
----                           -----
WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0}
SerializationVersion           1.1.0.1
BuildVersion                   6.2.9200.16481
PSVersion                      3.0
CLRVersion                     4.0.30319.1008
PSRemotingProtocolVersion      2.2


Install .NET 4

Option 1 (For All Apps)

  • Add New DWORD to registry
    • For 64-bit Applications
      • HKLM:SOFTWARE\Microsoft\.NETFramework
      • DWORD OnlyUseLatestCLR
      • Value 1
    • For 32-bit Applications
      • HKLM:SOFTWARE\Wow6432Node\Microsoft\.NETFramework
      • DWORD OnlyUseLatestCLR
      • Value 1

Gnome-sticky-notes-applet No reboot required


Option 2 (PowerShell Only)

Create a file name powershell.exe.config in the $PSHOME directory with the following content.

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


Gnome-sticky-notes-applet There are two file paths. One is for 64-bit and other is for 32-bit PowerShell. Just type $PSHome when in PowerShell console to get full path. You'll need to do for both if you want both versions to load .NET 4 at runtime on a 64-bit operating system.

x86 = C:\Windows\SysWOW64\WindowsPowerShell\v1.0
x64 = C:\Windows\System32\WindowsPowerShell\v1.0


Icon-Tip-Square-Green.png Windows default permissions will make it difficult to write a file to those locations. So create the file in a unsecured folder like C:\myscripts and use PowerShell to copy it to the location. You'll need to open PowerShell as Administrator (Right-Click Run as...).

Display PowerShell Console .NET Version

To display the current runtime version of .NET in a PowerShell console use the following command:
[Environment]::Version.ToString()

Before

[Environment]::Version.ToString()
2.0.50727.5448

OR

$PSVersionTable

After

[Environment]::Version.ToString()
4.0.30319.239

OR

$PSVersionTable


Related Articles

(HowTo) Setup PowerShell Module
(HowTo) Enable Remote Signed PowerShell Scripts


Sources

http://www.powergui.org/thread.jspa?threadID=11948
http://www.poshpete.com/powershell/enabling-net-4-in-your-powershell-environment