Using .NET and PowerShell classes

PowerShell · Expert · 14 min

When no cmdlet exists, the entire .NET library is one line away, and classes give you real types.

PowerShell runs on .NET, so every .NET type is available. Static members use [Type]::Member: [math]::Round(2.567, 2), [System.IO.Path]::GetExtension($f), [datetime]::Parse('2026-01-15'), [guid]::NewGuid(). Common namespaces may be shortened ([IO.Path]), and a using namespace System.Text line at the top of a script lets you write [StringBuilder]. Create instances with [Type]::new(args) (or the older New-Object).

PowerShell classes (since 5.0) define real .NET types with typed properties, constructors, methods, static members, inheritance and enums:

  • methods must declare a return type and use return explicitly; unlike functions, stray values are not output,
  • reference the instance with $this,
  • override ToString() to control how objects appear in strings.

Classes are useful for validated data structures and for advanced features like custom validation attributes and DSC resources. Limitations: a class must be defined before the code that uses it is parsed, and changes to a class in a module need a new session to reload.

Add-Type -TypeDefinition compiles C# source on the fly, for performance-critical code or P/Invoke calls into native APIs. Compiled types cannot be unloaded from the session, so rename them while iterating.

Example

[System.IO.Path]::GetExtension('report.final.csv')   # .csv
[math]::Sqrt(144)                                    # 12
[datetime]::Parse('2026-01-15').DayOfWeek            # Thursday
$sb = [System.Text.StringBuilder]::new()
[void]$sb.Append('fast ').Append('strings')
$sb.ToString()                                       # fast strings
class Server {
  [string]$Name
  [int]$Cpu
  Server([string]$name, [int]$cpu) { $this.Name = $name; $this.Cpu = $cpu }
  [bool] IsBusy() { return $this.Cpu -gt 80 }
  [string] ToString() { return "$($this.Name) ($($this.Cpu)%)" }
}
$s = [Server]::new('web1', 92)
$s.IsBusy()                                          # True
"$s"                                                 # web1 (92%)
Add-Type -TypeDefinition 'public static class Calc { public static int Twice(int x) { return x * 2; } }'
[Calc]::Twice(21)                                    # 42

Remember

  • [Type]::StaticMember and [Type]::new() reach all of .NET
  • Class methods need return types and explicit return
  • Add-Type compiles C# for speed or native interop

Watch out for

  • Expecting a class method to output values like a function: only the return value comes back.

Check yourself

  1. How do you create a new System.Text.StringBuilder in modern PowerShell?
  2. A class method contains `'debug'; return 5`. What does calling it return?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes

Using .NET and PowerShell classes · PowerShell · Topolearn