Could not load file or assembly

Question:

In my power shell script I am loading a custom assembly and then instantiating a class of that assembly by New-Object.

Assembly.LoadFile() executes successfully but New-Object statement gives the bellow exception.

Script:

This custom assembly references only the following assemblies

I tried explicitly loading the System.Runtime.Serialization dll like below. But same exception

Any idea?

Answer:

Don’t use LoadFile. Since you are using Powershell V2, the preferred method is Add-Type

http://www.dougfinke.com/blog/index.php/2010/08/29/how-to-load-net-assemblies-in-a-powershell-session/ has a good list of the different ways available to import a library into the current shell runspace.

http://technet.microsoft.com/en-us/library/hh849914.aspx is the technet documentaion on Add-Type and has examples including how to use static methods in a loaded library

Source:

Could not load file or assembly by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply