PowerShell Classes for Developers
From the classic way of creating objects of .NET classes or defining a custom .NET class, we have seen PowerShell extend .NET classes and types in numerous ways.
Join the DZone community and get the full member experience.
Join For FreeClasses in PowerShell have been a feature since long ago, and creating objects of these classes is nothing new. From the classic way of creating objects of .NET classes (like the MailMessage in Example 1 below) or to defining a custom .NET class (in Example 2 below), we have seen PowerShell extend .NET classes and types in numerous ways.
Example 1: Creating object of a .NET class
$message = New-Object System.Net.Mail.MailMessage
Example 2: Defining .NET class in PowerShell
$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);
}
}"
Add-Type -TypeDefinition $source
PowerShell Classes
With the new version PowerShell, you can create classes in PowerShell instead of just using .NET classes. Object Orientation is now available in your scripting language so that you can shorten your scripts and make them more maintainable.
I recently spoke about PowerShell Classes at PowerShell Conference Asia, 2015 and it seemed to spark a great deal of interest. So, I created two short videos to help you quickly ramp up your PowerShell skills to create PowerShell classes, objects, methods, overloads, scope, and a lot more. Check out the videos down below.
Video 1: Getting Started With PowerShell Classes
Video 2: Constructor, Methods, Overloading and Scope
Hope this helps you!
Opinions expressed by DZone contributors are their own.
Comments