PowerShell runtime exception - "could not load file or assembly"

13

This seems to be a common problem in PowerShell and Visual Studio, yet cases and solutions seem to vary a lot. Though seeing several similar questions, I didn't find a working solution for my issue yet.

The problem exists in the error message

Could not load file or assembly 'file:///C:\users\jenstmar\Desktop\WinSCP.dll' o r one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

The file location is invalid, as the .dll is supposed to be located in the same folder as the WinSCP installation. This location was changed to check that no rights or lack thereof, restricted me to use it.

The script line that causes the problem looks as following:

# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("C:\users\jenstmar\Desktop\WinSCP.dll") | Out-Null

This error comes in both PowerShell ISE and PS ISE(x86). I run PowerShell V3.0 in a Windows Enterprise 64 bit environment as local administrator. How can I fix this problem?

exception
powershell
winscp
winscp-net
asked on Stack Overflow Dec 10, 2012 by Mark Jenster • edited Jul 14, 2015 by Peter Mortensen

5 Answers

15

The file may be blocked from being loaded because of its origin (stored in NTFS streams). Check properties and see if the little unblock button is lit up. Once you unblock it perhaps it will load...

answered on Stack Overflow Dec 10, 2012 by Chris N • edited Jul 14, 2015 by Peter Mortensen
9

Solution in this case: Remove and download it all again. I even unblocked it on the first file, with no help. Glad this was on an internal machine.

answered on Stack Overflow Dec 13, 2012 by Mark Jenster
3

The following worked for me (from Stack Overflow question Add-Type load assembly from network UNC share error 0x80131515).

In files:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>
answered on Stack Overflow Jan 20, 2014 by wtjones • edited May 23, 2017 by Community
2
Import-Module : Could not load file or assembly 'file:/// *dll path*' or one of 
its dependencies. An attempt was made to load a program with an incorrect format.
At *script path*.ps1:68 char:2
+     Import-Module *module path*
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.ImportModuleCommand

If you see the above error, it might be the issue of running the Windows PowerShell ISE in 32 bit mode(Windows PowerShell ISE(x86)). You have to run the application Windows PowerShell ISE.

This application(Windows PowerShell ISE) is in 64 bit mode. When I executed the power shell command, its working fine for me.

I have been trying to fix this with different types solutions but it doesn't worked for me. If you running it 32 mode, switch it to 64 bit and try.

answered on Stack Overflow Oct 25, 2016 by Shri • edited Oct 25, 2016 by baldr
0

I would look at WinSCP.dll dependencies in ILDASM or dotPeek and then load those dependencies before loading WinSCP.dll. The problem is that you are running in PowerShell.exe (or powershell_ise.exe) fusion load context and the assemblies that WinSCP.dll aren't going to be found under these two exe's base dirs. So you will need to load the dependent assemblies before the CLR loader chokes because it can't find a required DLL. If you need help figuring out which assembly can't be found, check out the fuslogvw.exe tool.

BTW, as of PowerShell v2 you should use Add-Type -Path <path> instead of [S.R.A]::LoadFrom(...).

answered on Stack Overflow Dec 10, 2012 by Keith Hill

User contributions licensed under CC BY-SA 3.0