September 3rd, 2015

Creating Instances of PowerShell 5 Classes

Doctor Scripto
Scripter

Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating instances of Windows PowerShell 5.0 classes in Windows 10.

Microsoft Scripting Guy, Ed Wilson, is here. Today I am up early. I have the windows open, and I am enjoying the early morning breeze that precedes the midmorning sun and afternoon thunderstorms. I am looking over some of the comments about this week’s series of Hey, Scripting Guy! Blog posts, and I see that someone didn’t like the way I spelled Camero in my code.

Oh well, I probably should have stuck with BMW Z-3 or Z-4. Someone else mentioned that there are three-wheel cars, and that is probably true. In fact, there are even cars that are more like the Fred Flintstone mobiles that are foot peddled. It is a three wheeler, and I was actually going to buy one a while back. It looks like a very practical and sane vehicle. I mean, why do I need a 425 HP vehicle to travel at 3 MPH and sit in traffic for 30 minutes when I can do it in my Fred Flintstone mobile just as easily!

But, really both of these questions relate to class design, don’t they? For example, if I have a class of car that has a model of Camero, it is essential that I spell it correctly, and that is where the enum came into play yesterday.

I could have just as easily created a Model enum as I did a Make enum,  and assuming that I spell things correctly, everything is groovy. Now, if I don’t have a Model enum, I can end up with a Camero, a Camaro, a Camel,  and maybe even a Cemerio, which will totally mess things up when it comes time to do a Make/Model report.

   Note  This is the fourth post in a series about creating classes in Windows PowerShell 5.0. You should read the earlier
   posts prior to reading today's post:

Another Note  I talked about enums last week. If you need to review enums, you should read:

There are a couple of ways to create instances of Windows PowerShell 5.0 classes.

Use New-Object

Probably the way that makes the most sense, is to use the New-Object cmdlet. I mean, this is the way that we have created instances of classes in Windows PowerShell ever since Windows PowerShell 1.0 shipped. In fact, it was there even before Windows PowerShell 1.0 shipped. So, it has the virtue of simplicity, readability, and longevity. Yep, with all this going for it, we need to know this method.

I run the script that creates my Car class in Windows PowerShell 5.0. After I have run the script, I can go into the interactive Windows PowerShell pane in the ISE and use New-Object to create a new instance of the Car class. To make things useful, I store the returned object into a variable I call $car. This is shown here:

PS C:\> $car = New-Object car

PS C:\> $car

vin           :

numberOfDoors : 0

year          : 1/1/0001 12:00:00 AM

model         :

make          : 0

color         : 0

Use the New-Object cmdlet, the name of the class, and a variable to store the returned instance of the class (a Car object). Now, I can use straightforward property assignments to assign values for the various properties:

Image of command output

Another way to create a new instance of the Car class by using the New-Object cmdlet is to supply values for the properties when creating the class itself. As shown here, this technique actually reduces typing:

$car = New-Object car -Property @{vin=1234;year="1/1/2015";model="z-4";color=1;Make=5}

The following image shows the command and the output from the command :

Image of command output

Because the –Property parameter accepts <IDictionary> for input, I can create a hash table and store it in a variable. I can then supply that hash table to the New-Object command. This is not exactly like splatting, but it does simplify coding, and it makes the code easier to read. This technique is shown here:

PS C:\> $properties = @{vin=1234;year="1/1/2015";model="z-4";color=1;Make=5;NumberOfDoors=2}

PS C:\> $car = New-Object car -Property $properties

PS C:\> $car

vin           : 1234

numberOfDoors : 2

year          : 1/1/2015 12:00:00 AM

model         : z-4

make          : BMW

color         : Red

Use the New static method

Another way to create an instance of the Car class is to use the New static method that comes from the class. As it stands right now, because I have not created any constructors, the only thing I can do is create a new instance of the class. I then need to assign values to the various properties. When I call New, I get everything that I predefined in my class, including my static members:

PS C:\> $chevy = [car]::new()

PS C:\> $chevy

vin           :

numberOfDoors : 0

year          : 1/1/0001 12:00:00 AM

model         :

make          : 0

color         : 0

I then go through the process of filling out the properties:

PS C:\> $chevy.vin = 1234

PS C:\> $chevy.numberOfDoors = 2

PS C:\> $chevy.year = "1/1/2000"

PS C:\> $chevy.model = "volt"

PS C:\> $chevy.make = "chevy"

PS C:\> $chevy.color = 2

When I am done, I have the following:

PS C:\> $chevy

vin           : 1234

numberOfDoors : 2

year          : 1/1/2000 12:00:00 AM

model         : volt

make          : Chevy

color         : Blue

That is all there is to creating instances of Windows PowerShell 5.0 classes. Windows PowerShell Classes Week will continue tomorrow when I will talk about more cool stuff.

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 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Rohit Devmore

    Thank you. I have a script that uses this ::new() method to create instance. However, when we run script on server that has powershell V3, server admin gets an error “doesnt contan method  NEW”. What are alternative ways than using “::new” to create instace , since powershell V3 doesnt support? any suggestions please
    Server admin is not ready to upgrade powershell, since its PROD server.