{"id":73581,"date":"2015-09-03T00:01:00","date_gmt":"2015-09-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/03\/creating-instances-of-powershell-5-classes\/"},"modified":"2019-02-18T09:35:22","modified_gmt":"2019-02-18T16:35:22","slug":"creating-instances-of-powershell-5-classes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/creating-instances-of-powershell-5-classes\/","title":{"rendered":"Creating Instances of PowerShell 5 Classes"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about creating instances of Windows PowerShell&nbsp;5.0 classes in Windows 10.<\/span><\/p>\n<p>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&rsquo;s series of Hey, Scripting Guy! Blog posts, and I see that someone didn&rsquo;t like the way I spelled Camero in my code.<\/p>\n<p>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!<\/p>\n<p>But, really both of these questions relate to class design, don&rsquo;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.<\/p>\n<p>I could have just as easily created a <b>Model<\/b> enum as I did a <b>Make<\/b> enum, &nbsp;and assuming that I spell things correctly, everything is groovy. Now, if I don&rsquo;t have a <b>Model<\/b> enum, I can end up with a Camero, a Camaro, a Camel,&nbsp; and maybe even a Cemerio, which will totally mess things up when it comes time to do a Make\/Model report.<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> This is the fourth post in a series about creating classes in Windows PowerShell&nbsp;5.0. You should read the earlier <br \/>&nbsp; &nbsp;posts prior to reading today&#039;s post:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-powershell-5-classes\/\" target=\"_blank\">Introduction to PowerShell 5 Classes<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/adding-enums-to-powershell-5-classes\/\" target=\"_blank\">Adding Enums to PowerShell 5 Classes<\/a><\/li>\n<\/ul>\n<p><b>Another Note&nbsp;<\/b> I talked about enums last week. If you need to review enums, you should read:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/new-powershell-5-feature-enumerations\/\" target=\"_blank\">New PowerShell 5 Feature: Enumerations<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/working-with-enums-in-powershell-5\/\" target=\"_blank\">Working with Enums in PowerShell 5<\/a><\/li>\n<\/ul>\n<p>There are a couple of ways to create instances of Windows PowerShell 5.0 classes.<\/p>\n<h2>Use New-Object<\/h2>\n<p>Probably the way that makes the most sense, is to use the <b>New-Object<\/b> cmdlet. I mean, this is the way that we have created instances of classes in Windows PowerShell ever since Windows PowerShell&nbsp;1.0 shipped. In fact, it was there even before Windows PowerShell&nbsp;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.<\/p>\n<p>I run the script that creates my <b>Car<\/b> 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 <b>New-Object<\/b> to create a new instance of the <b>Car<\/b> class. To make things useful, I store the returned object into a variable I call <b>$car<\/b>. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $car = New-Object car<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $car<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 0<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p>Use the <strong>New-Object<\/strong> cmdlet,&nbsp;the name of the class, and a variable to store the returned instance of the class (a <b>Car<\/b> object). Now, I can use straightforward property assignments to assign values for the various properties:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-3-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-3-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Another way to create a new instance of the <b>Car<\/b> class by using the <b>New-Object<\/b> cmdlet is to supply values for the properties when creating the class itself. As shown here, this technique actually reduces typing:<\/p>\n<p style=\"margin-left:30px\">$car = New-Object car -Property @{vin=1234;year=&quot;1\/1\/2015&quot;;model=&quot;z-4&quot;;color=1;Make=5}<\/p>\n<p>The following image shows the command and the output from the command :<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-3-15-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-3-15-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Because the <b>&ndash;Property<\/b> parameter accepts <b>&lt;IDictionary&gt;<\/b> for input, I can create a hash table and store it in a variable. I can then supply that hash table to the <b>New-Object<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $properties = @{vin=1234;year=&quot;1\/1\/2015&quot;;model=&quot;z-4&quot;;color=1;Make=5;NumberOfDoors=2}<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $car = New-Object car -Property $properties<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $car<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1234<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 2<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/2015 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : z-4<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : BMW<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Red<\/p>\n<h2>Use the New static method<\/h2>\n<p>Another way to create an instance of the Car class is to use the <b>New<\/b> 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 <b>New<\/b>, I get everything that I predefined in my class, including my static members:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy = [car]::new()<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 0<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p>I then go through the process of filling out the properties:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.vin = 1234<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.numberOfDoors = 2<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.year = &quot;1\/1\/2000&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.model = &quot;volt&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.make = &quot;chevy&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy.color = 2<\/p>\n<p>When I am done, I have the following:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $chevy<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1234<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 2<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/2000 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : volt<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Chevy<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Blue<\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating instances of Windows PowerShell&nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[615,609,3,4,608,45],"class_list":["post-73581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-classes","tag-powershell-5","tag-scripting-guy","tag-scripting-techniques","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about creating instances of Windows PowerShell&nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=73581"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=73581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}