Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PowerShell Classes for Developers

DZone's Guide to

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.

· DevOps Zone ·
Free Resource

Read why times series is the fastest growing database category.

Classes 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!

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

Topics:
powershell ,developer ,classes ,object oriented programming

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}