How to reference .NET assemblies using PowerShell

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)

Leave a Comment