Invoke .Net 4 assembly with the assembly’s .config file using PowerShell

In a project developed using .Net 4, we needed to make some functionality available for scripting in PowerShell.

  • The first task was to configure PowerShell to be able to load .Net 4 assemblies:

The $pshome variable is the directory where the powershell.exe and its configuration file powershell.exe.config reside (the .config does not seem to be required on Win7 as it was not on my system). On 32-bit systems, the path is C:\Windows\System32\WindowsPowerShell\v1.0, 64-bit systems have C:\Windows\SysWOW64\WindowsPowerShell\v1.0.

Create a file called powershell.exe.config in a temp directory, and paste this code (found on SO)

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

Open a command prompt as administrator, and copy the file to PowerShell’s directory

copy .\powershell.exe.config

Restart PowerShell, and you are able to load .Net 4 assemblies.

  • Next, we need to set up the .Net environment so that it uses the assembly’s configuration file instead of PowerShell’s.

Since the executable that is loading the assembly is powershell.exe, it is thus looking for the powershell.exe.config. To use the correct config file, we need to set the current AppDomain’s APP_CONFIG_FILE property (found on SO):

$filename = "c:\path\to\MyAssembly.dll"
    "APP_CONFIG_FILE", $filename + ".config")
$assembly =

One Response to Invoke .Net 4 assembly with the assembly’s .config file using PowerShell

  1. […] configuring PowerShell to use an assembly’s config file and successfully loading assemblies an…, I found that assemblies referenced by the assembly’s config file were not found by the […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: