Introduction to PowerShell 5 Classes

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about Windows PowerShell 5 classes in Windows 10.

Microsoft Scripting Guy, Ed Wilson, is here. It is an interesting morning. We are up early, looking around, and trying to decide if the latest tropical storm is going to become a hurricane…and if it is going to hit or miss Florida.

Of course, in Central Florida, we will get a miss, but still it could mean several days of high winds, lots of rain, a bit of flooding, and perhaps some power outages. So we need to stock up on food and water, and fill up the vehicles with petrol. I am thinking we need to make sure we have lots of tea, scones, and maybe a box or two of biscotti. Throw in some fresh fruit, and I will be set for a week.

I will also make sure I have my extra batteries for my laptop charged up. I have a couple big UPSs, and a cell phone microcell, so maybe all will be good—as long as the Internet provider can keep its end of the pipe up and running.

Certainly, there is enough new stuff in Windows PowerShell 5.0 to keep me entertained for a week—or much longer if need be.

About Classes

One of the cool things about Windows PowerShell 5.0 is that it adds the ability to create a classes. So, what is cool about that? That might be hard to say really. For example, it might be important to know what a class is, where a class might be used, and so on.

In object-oriented programming, we use objects. That makes sense, I guess. Everything is an object, and objects can contain other objects, and so on. But where do these objects come from?

Well, they come from classes, and classes are the templates used to create an instance of a class.

Note  Classes are not objects, and objects are not classes. An object is an instance of a class.

A class may have static properties, or even static methods. These are hard-coded into the class, so I do not have to create an instance of the class to gain access to the static method. Instead, it always exists.

One of the really cool things in Windows PowerShell is that these static members (methods or properties) are designated by the double colon, so it is obvious if it is a static member. In the following example, I use the static Now property from the System.DateTime class, and it returns the current date and time:

PS C:\> [datetime]::Now

Friday, August 28, 2015 10:55:32 AM

To create a new instance of a DateTime class, I use one of the constructors that are available. When I have created a new instance of a DateTime class, I have a new DateTime object.

Note  Constructors are often required to create a new object from a class.

What are classes for

Classes can be thought of as containers for methods, properties, enums, and other things. A class becomes a template for an object. For example, suppose we want to create a template for a car. We need to describe the properties of the car:

VIN

Make (Enum)

Model

Color (Enum)

Year

Number of wheels::Static

Number of doors

Type of top

It also has a number of methods, such as:

DriveDownTheRoad::Static

StopAtAStopSign

PlayMusic

SetCruiseControl

Park::Static

There may be events associated with the class. For example, in our car class there may be the following events:

RunOutOfGass

FailureToStart

RunARedLight

GoToFast

It also makes sense that we would create a few enums for our class. For example, color would be a great enum, because in general, cars only come in certain colors. Also type of top might be a good enum because there are convertibles and hardtops (and in the old days, vinyl tops, moon roofs, and T-tops). We might even define an enum for make of car because there are only so many different carmakers. So our enums might look like the following:

Color

TopType

Make

We might also define a few static properties on our car class. All cars have four wheels, and so number of wheels might make a good static property:

NumberWheels

We could have a few static methods also. All cars should implement the DriveDownTheRoad method, and the Park method. So our static methods are:

DriveDownTheRoad

Park

This is exactly the process used for creating a class in Windows PowerShell. The first thing I do is design my class. I need to think about what describes it, what it does, and what its limitations are. After I have spent a decent amount of time designing my class, I begin to write the code—but usually not until I have taken some time to mull over what it is I am trying to do and how I will use the class in the future.

There is an introduction to using classes. Windows PowerShell Classes Week will continue tomorrow when I will talk about implementing a class.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon