63

I am a C# .NET developer/architect and understand that it uses objects (.NET objects) and not just streams/text.

I would like to be able to use PowerShell to call methods on my .NET (C# library) assembies.

How do I reference an assembly in PowerShell and use the assembly?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Russell
  • 17,481
  • 23
  • 81
  • 125

2 Answers2

70

With PowerShell 2.0, you can use the built in Cmdlet Add-Type.

You would just need to specify the path to the dll.

Add-Type -Path foo.dll

Also, you can use inline C# or VB.NET with Add-Type. The @" syntax is a HERE string.

C:\PS>$source = @"
    public class BasicTest
    {
        public static int Add(int a, int b)
        {
            return (a + b);
        }

        public int Multiply(int a, int b)
        {
            return (a * b);
        }
    }
    "@

    C:\PS> Add-Type -TypeDefinition $source

    C:\PS> [BasicTest]::Add(4, 3)

    C:\PS> $basicTestObject = New-Object BasicTest 
    C:\PS> $basicTestObject.Multiply(5, 2)
Andy Schneider
  • 8,516
  • 6
  • 36
  • 52
63

Take a look at the blog post Load a Custom DLL from PowerShell:

Take, for example, a simple math library. It has a static Sum method, and an instance Product method:

namespace MyMathLib
{
    public class Methods
    {
        public Methods()
        {
        }

        public static int Sum(int a, int b)
        {
            return a + b;
        }

        public int Product(int a, int b)
        {
            return a * b;
        }
    }
}

Compile and run in PowerShell:

> [Reflection.Assembly]::LoadFile("c:\temp\MyMathLib.dll")
> [MyMathLib.Methods]::Sum(10, 2)

> $mathInstance = new-object MyMathLib.Methods
> $mathInstance.Product(10, 2)
cyberbemon
  • 3,000
  • 11
  • 37
  • 62
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    The link is loading here. I will update my post and copy the relevant part from the it. – Darin Dimitrov Jun 20 '10 at 13:26
  • Thanks for the text. :) I know now why I couldn't find the answer when I was searching. I'll try it and let you know how I go. :) – Russell Jun 20 '10 at 13:30
  • You can also use the `Add-Type` cmdlet. – Joey Jun 20 '10 at 13:36
  • @Johannes Rossel - How do I use the Add-Type cmdlet? From the get-help command it appears to be for dynamic assemblies? – Russell Jun 20 '10 at 13:44
  • 8
    @Russell: `add-type -path .\foo.dll`. You can also use it to directly compile code. – Joey Jun 20 '10 at 13:54
  • @Johannes, you should add it as a new answer. I'll vote it up ;) I think if possible standard PowerShell means should be used and pure .NET only if needed. – stej Jun 20 '10 at 20:51
  • @stej: Same here. Although `Add-Type` didn't exist prior to v2, if I'm not mistaken, so it deserves a mention, although no separate answer in my humble opinion. – Joey Jun 20 '10 at 21:20
  • 4
    @Johannes, Darin is basically right. His solution will work. However, if somebody else will come here (e.g. from google), he will see his answer and will not read through the comments. That's why I think there should be another answer with `Add-Type`. – stej Jun 20 '10 at 22:08
  • @stej: You can give it, then. You need more rep than me in any case ;-) – Joey Jun 20 '10 at 22:17
  • hi guys, thanks the add-type is what I am looking for. :) It would be good to put it in a separate question as Add-type is a nicer solution (more built-into powershell), albeit for V2.0 and above. – Russell Jun 21 '10 at 00:03
  • 1
    404 on the link at the start of your post. – Sean B Sep 24 '15 at 23:32
  • Does this not work for .NET Standard dll's? I'm getting this error when I run the `Add-Type -Path "C\some.dll"` `Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.` – Kellen Stuart Nov 23 '19 at 01:42
  • This example looks allot like Lee Holmes - https://www.leeholmes.com/blog/2006/10/27/load-a-custom-dll-from-powershell/. – Adam Jan 31 '20 at 00:01