PowerShell Cmdlet Deep Dive: Add-Type
Welcome back to Wahmans PowerShell Blog! Today we’ll explore a mighty and often underutilized cmdlet: Add-Type
. This command lets you bring the power of .NET into your PowerShell scripts by adding a .NET class to your PowerShell session.
Microsoft’s Official Description: Adds a Microsoft .NET class to a PowerShell session.
This foundational capability can be used to extend your scripts with performance-optimized or specialized functionality not provided by PowerShell out of the box. Let’s look at how to use Add-Type
in several practical scenarios — from beginner to more advanced use cases.
Example 1: Creating a Simple .NET Class
This example shows how to create and use a simple C# class directly within PowerShell.
$classDefinition = @"
public class Greeter {
public string SayHello(string name) {
return $"Hello, {name}!";
}
}
"@
Add-Type -TypeDefinition $classDefinition
$greeter = New-Object Greeter
$greeter.SayHello("Alice")
Explanation: You’re defining a class called Greeter
with a method SayHello
and calling it via PowerShell.
Example 2: Generate a Random Number Using .NET’s Random Class
PowerShell has its own ways for generating random numbers, but here’s how to do it using .NET via Add-Type
.
Add-Type -AssemblyName System
$random = New-Object System.Random
$random.Next(1, 100)
Explanation: Using Add-Type allows access to the broader .NET Base Class Library, like System.Random
.
Example 3: Create Custom Windows Forms with .NET
PowerShell doesn’t natively support GUI, but Add-Type
allows you to build it using Windows Forms. Here, we create a basic form with a button:
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "My First Form"
$form.Width = 300
$form.Height = 200
$button = New-Object System.Windows.Forms.Button
$button.Text = "Click Me"
$button.Width = 100
$button.Height = 50
$button.Top = 50
$button.Left = 100
$button.Add_Click({ [System.Windows.Forms.MessageBox]::Show("Hello from PowerShell!") })
$form.Controls.Add($button)
$form.ShowDialog()
Explanation: A quick way to add a basic GUI to your scripts using the Windows Forms .NET library.
Example 4: Using Add-Type to Call Native Win32 API
This more advanced example demonstrates calling a Windows API to perform an operation outside of built-in PowerShell functionality.
$signature = @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll")] public static extern bool LockWorkStation();
}
"@
Add-Type -TypeDefinition $signature
[User32]::LockWorkStation()
Explanation: You declared and used a native function (LockWorkStation
) from user32.dll
to lock the PC. This unlocks (pun intended) a world of low-level OS functionalities from within PowerShell.
Wrapping Up
Add-Type
might look simple at first, but it gives you unmatched flexibility by letting you seamlessly blend .NET with PowerShell. Whether you want to write quick ad-hoc classes, create GUIs, or tap into native Windows APIs, Add-Type
is your gateway.
Happy scripting, and I will see you in the next post!
Leave a Reply