Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating a simple class in Windows PowerShell 5.0 in Windows 10.
Microsoft Scripting Guy, Ed Wilson, is here. The good news is that so far the weather person has been wrong—every single day for the last five days in a row. The bad news is that, well, unfortunately, I believed the weather person, and so I have been huddled in at home, looking out the windows for a storm that never arrived.
We have had a little bit of rain, but not even the normal afternoon thunder storms. Guess I shouldn’t complain, but I was hoping to go take pictures of alligators over the weekend.
The other good news is that I have had lots of time to play around with Windows PowerShell 5.0 in Windows 10 this weekend.
Note This is the second post in the series about creating classes in Windows PowerShell 5.0. You should read
Introduction to PowerShell 5 Classes prior to reading today’s post.
So yesterday, I wrote about classes. In general, and I talked about the process of designing a class. I cannot stress this too much:
If you are going to create a class in Windows PowerShell, you must design it.
Nothing is worse than to start writing a module, create a bunch of classes, and then all of a sudden, realize that you need to add (or worse yet, change) one or more properties.
I say worse yet, because I always have the option of inheriting a class and adding properties to it. It may mess up my schema or my design, but I can inherit a class. What messes up things to the point that it requires major rewriting is deciding that a property I implemented as a single string suddenly needs to be an array...or that something that I implemented as a static property suddenly needs to be dynamically created. It is imperative that I properly design my class at the outset.
Dr. Scripto says: One hour of design work will save 10 hours of rewrite work later.
Use the Class keyword
The first thing I need to do is to use the Class keyword. This is what creates the class. The syntax is exactly the same as creating a function:
And there is stuff in the middle. I call these sandwich commands because there is bread on the outside, and the middle is where all the exciting stuff happens.
So, to create the Car class, I type:
Then I add my properties. There are several options here. The default terminator is the end-of-line (carriage return line feed), but I can also use a semicolon. Depending on my mood, I prefer the invisible end-of-line terminator, and I place each property command on its own line.
Note In other things, the Windows PowerShell automatic type system is great, and it works remarkably well. However,
when creating classes that you really need—as in, you should (in fact, nearly must) specify the data type for each property.
If you don’t, everything will simply be a System.Object, and will be pretty much useless. If you have a string, an int, or a
DateTime object, you must specify that type so that things will work well.
The first property I want to specify is the vehicle identification number (VIN). There probably is an exact pattern for this, and I could use a validation routine for that if it really mattered. For my example, it doesn’t matter, and I have no intention on typing more than a few numbers here.
But when I have numbers separated by dashes, those numbers really become strings—and that is the point. Also, each vehicle has a single VIN, so I do not type an array. It is a single string. Here is my property (it is a System.String):
Note The data type goes in square brackets, so that it will cast or constrain the data to be the specified type.
There is a new keyword in Windows PowerShell 5.0 I can use when creating classes: Static. All cars have four wheels (at least for this example), and so, I simply assign that value here. It will be a static property, meaning it is always available. Even if I don’t create an instance of the class Car, I will be able to figure out that cars have four wheels.
Because the numberOfWheels property is the number 4, I cast the property to be an integer by using [int]. I could have made it an [int16] instead of the default [int32] that [int] creates, but hey, I am not being that specific.
static [int]$numberOfWheels = 4
Note What’s the difference between [int16] and [int32]?The short answer is the size of the number that it will hold.
The specific answer? Check the MaxValue static property, as shown here:
PS C:\> [int16]::MaxValue
PS C:\> [int32]::MaxValue
Now, I need to define the numberOfDoors property. It could be 2, 4, 5, or more. So I also cast it as an [int]. But because the value could be a lot of different things, I do not make it static. This is shown here:
The Year property could also be an [int]. But I decided to make it a [DateTime] object so that I would have access to the properties and methods that a System.DateTime object exposes. I only need to cast the value (constrain) as [datetime], and when I put in my year, Windows PowerShell will convert it to a System.DateTime object. It will fill in other stuff as required. This is shown here:
Now I need to add in the Model of the car. This is a simple string:
So that is it. Here is the complete class:
static [int]$numberOfWheels = 4
Create an instance of the class
I can use the New-Object cmdlet to create an instance of the Car class:
$chevy = New-Object car
I can use the Static property operator to view my static property:
PS C:\> $chevy::numberOfWheels
The class and the output from the class are shown here in the Windows PowerShell ISE:
Now I can simply assign values to the new Car object that is stored in my $chevy variable:
PS C:\> $chevy.vin = 12345
PS C:\> $chevy.numberOfDoors = 2
PS C:\> $chevy.year = "5/1/2015"
PS C:\> $chevy.model = "Camero"
When I view the contents of the $chevy variable, I can see my Car object:
PS C:\> $chevy
vin numberOfDoors year model
--- ------------- ---- -----
12345 2 5/1/2015 12:00:00 AM Camero
That's how you create a simple class in Windows PowerShell 5.0. Windows PowerShell Classes Week will continue tomorrow when I will talk about adding more stuff to the Windows PowerShell class.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy