{"id":82,"date":"2011-01-12T23:11:08","date_gmt":"2011-01-13T06:11:08","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=82"},"modified":"2011-01-12T23:11:08","modified_gmt":"2011-01-13T06:11:08","slug":"creating-an-array-of-custom-objects-in-powershell","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=82","title":{"rendered":"Creating an Array of Custom Objects in Powershell"},"content":{"rendered":"<p>Have you ever worked with arrays and felt you weren&#8217;t able to put in all the data you wanted? Have you tried making two arrays and syncing all the data between the two? Sometimes you just need to store more than one thing. The answer is to make your own custom object, and then put that into the array. It really is easier than you might think.<\/p>\n<p>First, you need to make a new system object. To do this we&#8217;ll use the New-Object command. Now if you know what type of object, as defined by .NET, that you would like to create, then you can specify it here. But the point here is that we want to make our own object. So we can make a generic, blank &#8220;object&#8221; by using a system.object.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$myObject<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">New-Object<\/span> System.Object<\/div><\/div>\n<p>This makes a new object, but a new object isn&#8217;t really anything at all. Think of an object as just that, something that you can feel, that is tangible. If I were to tell you that you were holding an object, what would that mean to you? Probably nothing until you had some idea of what it was. Let&#8217;s pretend that what I just handed you is a user&#8217;s workstation, now this object has some meaning,but past giving it a name,we still haven&#8217;t defined what the object really is. Objects have properties that define them. In the case of a computer there are things like its manufacturer, how fast its processor is, and how much memory it has. So we want to track all of that in our object.<\/p>\n<p>In Powershell what we need to do is add these properties to our object. We can do this with the Add-Member command. We need to tell it what type of property we are adding, what it&#8217;s called and what its value is.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$myObject<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Name <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Ryan_PC&quot;<\/span><br \/>\n<span class=\"re0\">$myObject<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Manufacturer <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Dell&quot;<\/span><br \/>\n<span class=\"re0\">$myObject<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> ProcessorSpeed <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;3 Ghz&quot;<\/span><br \/>\n<span class=\"re0\">$myObject<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Memory <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;6 GB&quot;<\/span><\/div><\/div>\n<p>Now we have a full object. It contains several properties that serve to define it and seperate it from other objects, even other PCs. We can work with the object in Powershell by calling it as a whole or by grabing any single property.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myObject<\/span><br \/>\n<br \/>\nName &nbsp; &nbsp; Manufacturer &nbsp; ProcessorSpeed &nbsp; Memory<br \/>\n<span class=\"sy0\">----<\/span> &nbsp; &nbsp; <span class=\"sy0\">------------<\/span> &nbsp; <span class=\"sy0\">--------------<\/span> &nbsp; <span class=\"sy0\">------<\/span><br \/>\nRyan_PC &nbsp;Dell &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"nu0\">3<\/span> Ghz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">6<\/span> GB<br \/>\n<br \/>\n<span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myObject<\/span>.Memory<br \/>\n<br \/>\n6GB<br \/>\n<br \/>\n<span class=\"sy0\">&gt;<\/span> Test<span class=\"sy0\">-<\/span>Connection <span class=\"re0\">$myObject<\/span>.Name <span class=\"kw5\">-quiet<\/span><br \/>\n<br \/>\nTrue<br \/>\n<br \/>\n<span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myObject<\/span>.Manufacturer <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;HP&quot;<\/span><br \/>\n<span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myObject<\/span><br \/>\n<br \/>\nName &nbsp; &nbsp; Manufacturer &nbsp; ProcessorSpeed &nbsp; Memory<br \/>\n<span class=\"sy0\">----<\/span> &nbsp; &nbsp; <span class=\"sy0\">------------<\/span> &nbsp; <span class=\"sy0\">--------------<\/span> &nbsp; <span class=\"sy0\">------<\/span><br \/>\nRyan_PC &nbsp;HP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"nu0\">3<\/span> Ghz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">6<\/span> GB<\/div><\/div>\n<p>So now that you have this really cool object and can work with, you will of course want to have many, many more, and keep them well\u00a0organized and in one place.\u00a0 You can do this, and\u00a0it too is very easy.\u00a0 Simply take your custom objects, and put them into an array.\u00a0 To show you, lets first make two more objects.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$myObject2<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">New-Object<\/span> System.Object<br \/>\n<br \/>\n<span class=\"re0\">$myObject2<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Name <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Doug_PC&quot;<\/span><br \/>\n<span class=\"re0\">$myObject2<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Manufacturer <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Dell&quot;<\/span><br \/>\n<span class=\"re0\">$myObject2<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> ProcessorSpeed <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;2.6 Ghz&quot;<\/span><br \/>\n<span class=\"re0\">$myObject2<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Memory <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;4 GB&quot;<\/span><br \/>\n<br \/>\n<span class=\"re0\">$myObject3<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">New-Object<\/span> System.Object<br \/>\n<br \/>\n<span class=\"re0\">$myObject3<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Name <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Julie_PC&quot;<\/span><br \/>\n<span class=\"re0\">$myObject3<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Manufacturer <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;Compaq&quot;<\/span><br \/>\n<span class=\"re0\">$myObject3<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> ProcessorSpeed <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;2.0 Ghz&quot;<\/span><br \/>\n<span class=\"re0\">$myObject3<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Add-Member<\/span> <span class=\"sy0\">-<\/span><span class=\"kw2\">type<\/span> NoteProperty <span class=\"kw5\">-name<\/span> Memory <span class=\"kw5\">-Value<\/span> <span class=\"st0\">&quot;2.5 GB&quot;<\/span><\/div><\/div>\n<p>Now we have three different objects,\u00a0 so let&#8217;s create an empty array to place them in.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$myArray<\/span> <span class=\"sy0\">=<\/span> <span class=\"sy0\">@<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>Okay, so now is simply adding the objects into the array.\u00a0 You can use the += operator to do it.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$myArray<\/span> <span class=\"sy0\">+=<\/span> <span class=\"re0\">$myObject<\/span><br \/>\n<span class=\"re0\">$myArray<\/span> <span class=\"sy0\">+=<\/span> <span class=\"re0\">$myobject2<\/span><span class=\"sy0\">,<\/span> $myObject3<\/div><\/div>\n<p>Notice that you can add more than one object to an array at once, if you like.\u00a0<\/p>\n<p>Now that this is complete we have a list of all our custom objects.\u00a0 This can be displayed nicely on the screen or even sent to out-gridview to give you a nice Excel-like view which can be searched, sorted, and filtered.\u00a0 You can also use the array in a ForEach statement to run code against.\u00a0 Keep in mind that you can also create the array of objects by adding one at a time in any given loop.\u00a0 This is exactly how I create the server lists for the companies I work for.\u00a0 It&#8217;s also worth stating that the &#8220;Add-Member&#8221; commandlet must have the value given, but you can simply give and empty string &#8220;&#8221; if you want to define it later.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myArray<\/span><br \/>\n<br \/>\nName &nbsp; &nbsp; &nbsp; &nbsp; Manufacturer &nbsp; &nbsp;ProcessorSpeed &nbsp; Memory<br \/>\n<span class=\"sy0\">----<\/span> &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">------------<\/span> &nbsp; &nbsp;<span class=\"sy0\">--------------<\/span> &nbsp; <span class=\"sy0\">------<\/span><br \/>\nRyan_PC &nbsp; &nbsp; &nbsp;Dell &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">3<\/span> Ghz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">6<\/span> GB<br \/>\nDoug_PC &nbsp; &nbsp; &nbsp;Dell &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">2.6<\/span> Ghz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">4<\/span> GB<br \/>\nJulie_PC &nbsp; &nbsp; Compaq &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">2.0<\/span> Ghz &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"nu0\">2.5<\/span> GB<br \/>\n<br \/>\n<span class=\"sy0\">&gt;<\/span> <span class=\"re0\">$myArray<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">Select-Object<\/span> name<br \/>\n<br \/>\nName<br \/>\n<span class=\"sy0\">----<\/span><br \/>\nRyan_PC<br \/>\nDoug_PC<br \/>\nJulie_PC<\/div><\/div>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever worked with arrays and felt you weren&#8217;t able to put in all the data you wanted? Have you tried making two arrays and syncing all the data between the two? Sometimes you just need to store more than one thing. The answer is to make your own custom object, and then put that into the array. It really is easier than you might think.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/82"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}