LoadFrom Exception: “An attempt was made to load a program with an incorrect format.”

While trying to load an assembly into PowerShell recently, an exception occurred,

“Exception calling “LoadFrom” with “1” argument(s): “Could not load file or assembly ‘file:///…’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

The cause of the problem was a mismatch in the platform target of the assembly. The assembly targets “x86” for example, rather than “Any CPU” or “x64” – the latter is the default PowerShell platform on Windows 8+ and Windows 2012+.

To correct the problem you either need to recompile the assembly using a more appropriate platform target (see the Visual Studio project’s property Build tab) or you need to run the 32-bit version of PowerShell:

%windir%\syswow64\Windowspowershell\v1.0\powershell.exe

or, for ISE:

"%windir%\syswow64\Windowspowershell\v1.0\powershell_ise.exe"

Windows 8+ runs the x64 version of PowerShell by default when launched from the Windows Menu and doesn’t even show an x86 icon. To correct this, you need to show the Administrative Tools as described in Starting the 32-Bit Version of Windows PowerShell.

Join the Conversation

3 Comments

Your email address will not be published. Required fields are marked *

  1. I get the same error when I try to run the powershell script with admin rights by 64 bit powershell on a windows 10 machine. I need to query and enable WindowsOptionalFeatures which requires elevated rights (admin). This command will not work on a 32 bit powershell. But when executed powershell from NSIS i get this error. I don’t know the correct way of calling the powershell from NSIS to get this working. Please help me out. Thanks!

  2. Thanks a ton for this. Was tearing out my hair trying to figure out why my Powershell script seemingly suddingly stopped working.