Add-Type

Understanding Add-Type in PowerShell: Add .NET Power to Your Scripts

Welcome back to Wahmans PowerShell Blog! Today, we’re diving into a powerful cmdlet that enables you to go beyond traditional PowerShell scripting: Add-Type.

According to Microsoft Docs, Add-Type “Adds a Microsoft .NET class to a PowerShell session”. That might sound simple, but this small statement hides a lot of power. Add-Type allows you to define and use custom .NET classes right inside your scripts, opening a world of possibilities when built-in PowerShell cmdlets just aren’t enough.

Why Use Add-Type?

PowerShell is built on .NET, and Add-Type is your bridge to leverage .NET classes and methods directly. You can define new types using C# code or load precompiled assemblies. This is particularly useful when you want to:

  • Inject high-performance code
  • Use existing .NET libraries
  • Write custom functionality not otherwise available

Let’s Look at Some Examples

Example 1: Create a Simple .NET Class (Beginner Level)

Let’s create a simple .NET class to represent a greeting system.

Add-Type @"
public class Greeter {
    public string SayHello(string name) {
        return $"Hello, {name}!";
    }
}
"@

$greet = New-Object Greeter
$greet.SayHello("PowerShell User")

This defines a class called Greeter with a method SayHello. You can now use it like any other object in PowerShell.

Example 2: Use System.Windows.Forms to Show a MessageBox (Intermediate)

Need to show a GUI message box? Here’s how:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Hello from Windows Forms in PowerShell!")

This uses a built-in .NET Assembly to show a standard message box popup. No C# code required, just access to the class!

Example 3: Load External Assembly (Intermediate)

If you’ve got a precompiled .NET assembly (DLL), you can load it like this:

$dllPath = "C:\Path\To\YourLibrary.dll"
Add-Type -Path $dllPath

# Assuming the DLL has a class called Utility.Tools
$tool = New-Object Utility.Tools
$tool.Run()

This is great when working with custom or third-party libraries compiled in other environments.

Example 4: Add Performance-Optimized Math Function (Advanced)

Let’s use some inline C# to create a performance-critical math function:

Add-Type @"
public class MathUtil {
    public static long Fibonacci(int n) {
        if (n <= 1) return n;
        long a = 0, b = 1;
        for (int i = 2; i <= n; i++) {
            long temp = a + b;
            a = b;
            b = temp;
        }
        return b;
    }
}
"@

[MathUtil]::Fibonacci(40)

This method is far quicker than writing the same logic in pure PowerShell, especially for computational functions like Fibonacci.

Wrapping Up

The Add-Type cmdlet is your gateway to a more powerful PowerShell. Whether you’re creating simple objects or importing full assemblies, it’s a great tool to keep in your scripting toolbox.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *