Home » Add-Type PowerShell Cmdlet

Add-Type PowerShell Cmdlet

The Add-Type cmdlet in PowerShell adds a Microsoft .NET class to a PowerShell session. It lets you define a Microsoft .NET Core class in your PowerShell session.

In this article, we will discuss about Add-Type cmdlet, its syntax and practical examples.

Syntax for Add-Type Cmdlet

The basic syntax for Add-Type cmdlet in PowerShell is as follows:

Add-Type [-AssemblyName] <string> [-Namespace <string>] [-Name <string>] [-MemberDefinition <string[]>] [-PassThru] [-UsingNamespace <string[]>] [<CommonParameters>]
  • -AssemblyName: Specifies the name of the assembly that contains the type to be added. This can be the name of a .NET assembly or the path to a .NET assembly DLL file.
  • -Namespace: Specifies the namespace of the type to be added. This is optional.
  • -Name: Specifies the name of the type to be added. This is optional.
  • -MemberDefinition: Specifies the definition of the members (properties, methods, etc.) of the type to be added. This is optional.
  • -PassThru: Returns the type object that was added. By default, Add-Type does not generate any output.
  • -UsingNamespace: Specifies additional namespaces to be imported. This is optional.

How to Add a .NET Class to a Session

To add a .NET class to a session, run the following script that adds the CalculatorApp class to the session by specifying source code.

$Source = @"
public class CalculatorApp
{
  public static int Add(int a, int b)
    {
        return (a + b);
    }
  public int Multiply(int a, int b)
    {
    return (a * b);
    }
}
"@

# Add the .NET class to the session
Add-Type -TypeDefinition $Source

# Call the Add() method of CalculatorApp
[CalculatorApp]::Add(1,5)

$object = New-Object CalculatorApp
$object.Multiply(4,3)

Result:

6
12

In the above PowerShell script, the $Source variable stores the CalculatorApp class source code. The class has a static Add() method and a non-static method called Multiply().

The Add-Type cmdlet adds the class to a session. It uses the -TypeDefinition parameter to specify the code in the $Source variable.

The Add() static method of the CalculatorApp class uses the double-colon character (::) to specify a static member of the class. It perform the sum of two numbers and displayed it.

The New-Object cmdlet instantiates an instance of the CalculatorApp class and store it in $object. $object uses the Multiply method to multiply two numbers.

How to Add Types From an Assembly

To add the classes form the assembly file Calc.dll to the current session, run the following script.

$CalcType = Add-Type -AssemblyName *Calc* -PassThru

The $CalcType variable stores an object created with the Add-Type cmdlet. The Add-Type uses the AssemblyName parameter to specify the name of the assembly. The asterisk (*) wildcard character allows you to get the correct assembly. The Passthru parameter generates objects that represents the classes that are added to the session.

Conclusion

I hope the above article on Add-Type cmdlet in PowerShell is helpful to you. The Add-Type cmdlet is useful when you need to define custom .NET types or leverage existing .NET types within your PowerShell scripts.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.