Understanding Classes in PowerShell – Part 03 – Adding Enums

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 02 of series can be read at here.

Adding Enums to PowerShell Classes

What are Enums

Sometimes, it makes sense to use Enums. After all, there can be so many variations of a particular property and coding them in advance helps in automatic parameter validation. When applied as a type constraint for a property in a Windows PowerShell class, an enum will generate an error if the value supplied is not a valid enumeration.

For example, let’s consider a property called color. If we stick to basic color such as red or blue, it is great. We can limit our search by constraint such as red and it will return all matching results. But if we use Scarlet, imperial red, indian red, spanish red, rusty red, ruby, chili red, redwood and so on, it can get messy quickly.  …Continue Reading

Understanding Classes in PowerShell – Part 02 – Adding Constructors

This blog post is continuation of series of posts on understanding classes in PowerShell. Part 01 of series can be read at here.

What is an Constructor

Constructor is an type of method or function which is called only when the object is created. It is never called after that no matter how many times you called the method or function.

The most common use of the constructor is to initialize properties related to the Object. As in .NET, the constructor must use the same name as the class.

For example, building on our previous example of class Dog, we can build a constructor as: …Continue Reading

Understanding Classes in PowerShell – Part 01 – Defining classes

Starting in Windows PowerShell 5.0, Windows PowerShell adds language for defining classes and other user-defined types. It is achieved by using the similar syntax and semantics being in use by other major object oriented programming languages. The goal is to enable developers (which I know, hate default syntax format and looking to build on their existing knowledge) and IT Professionals (to use more of their knowledge and make use of more formal coding mechanisms to achieve better code) to embrace Windows PowerShell for a wider variety of use cases. The use cases can be Desired State Configuration (DSC) resources, Generate and Handle exceptions, Introducing custom types, refactoring and re-using same code etc. This blog post is intended to cover PowerShell classes and their implementation in detail. This will be spread over multiple posts as this is a huge topic. …Continue Reading

Understanding default encoding and Change the same in PowerShell

This blog post is to discuss output encoding format used when data is passed from one PowerShell cmdlet or to other applications. This is a rarely understood feature unless you are trying to write some module which integrates PowerShell with another software. 

Passing output between PowerShell cmdlets

The Strings inside PowerShell are 16-bit Unicode, instances of .NET’s System.String class. So by default, when you pipe output from one cmdlet to another, it is passed as …Continue Reading