{"id":3885,"date":"2013-04-04T00:01:00","date_gmt":"2013-04-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/04\/why-powershell-uses-objects\/"},"modified":"2013-04-04T00:01:00","modified_gmt":"2013-04-04T00:01:00","slug":"why-powershell-uses-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/why-powershell-uses-objects\/","title":{"rendered":"Why PowerShell Uses Objects"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft PowerShell MVPs, Don Jones and Jeffery Hicks, show the flexibility and the power of Windows PowerShell objects.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual <strong>PowerTip<\/strong>. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off the book being excerpted that day. Remember that the code is valid only for the day the excerpt is posted. The coupon code is also valid for a second book from the Manning collection.<\/p>\n<p class=\"Body\" align=\"left\">Today, the excerpt is from <a href=\"http:\/\/www.manning.com\/jones3\/\" target=\"_blank\">Learn Windows PowerShell 3 in a Month of Lunches, Second Edition<br \/><\/a><span style=\"font-size: 12px\">&nbsp; By Don Jones and Jeffery Hicks<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6521.hsg-4-4-13-1.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Photo of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6521.hsg-4-4-13-1.jpg\" alt=\"Photo of book cover\" \/><\/a><\/p>\n<p>The use of objects in Windows PowerShell can be one of its most confusing elements, but at the same time it&rsquo;s one of the critical concepts, affecting everything you do in &ldquo;the shell.&rdquo; We&rsquo;ve tried different explanations over the years, and we&rsquo;ve settled on a couple that work well for distinctly different audiences. This blog, based on Chapter 8 of <a href=\"http:\/\/www.manning.com\/jones3\/\" target=\"_blank\">Learn Windows PowerShell 3 in a Month of Lunches<\/a>, shows you how Windows PowerShell uses objects.<\/p>\n<p class=\"Body1\" align=\"left\">One of the reasons why Windows PowerShell uses objects to represent data is, well, you have to represent data somehow, right? Windows PowerShell could have stored that data in a format like XML, or perhaps its creators could have decided to use plain-text tables. But they had some specific reasons why they didn&rsquo;t take that route.<\/p>\n<p class=\"Body\" align=\"left\">The first reason is that Windows itself is an object-oriented operating system&mdash;or at least, most of the software that runs on Windows is object oriented. Choosing to structure data as a set of objects is easy because most of the operating system lends itself to those structures.<\/p>\n<p class=\"Body\" align=\"left\">Another reason to use objects is because they ultimately make things easier on you and give you more power and flexibility. For the moment, let&rsquo;s pretend that Windows PowerShell doesn&rsquo;t produce objects as the output of its commands. Instead, it produces simple text tables, which is what you probably thought it was doing in the first place. When you run a command like <strong>Get-Process<\/strong>, you&rsquo;re getting formatted text as the output:<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">PS C:\\&gt; get-process<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;&nbsp; 1876&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4340&nbsp;&nbsp;&nbsp; 52&nbsp;&nbsp;&nbsp; 11.33&nbsp;&nbsp; 1920 conhost<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 792&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2260&nbsp;&nbsp;&nbsp; 22&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp; 2460 conhost<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 828&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2284&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp;&nbsp;&nbsp; 0.25&nbsp;&nbsp; 3192 conhost<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 574&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12&nbsp;&nbsp;&nbsp;&nbsp; 1864&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3896&nbsp;&nbsp;&nbsp; 43&nbsp;&nbsp;&nbsp;&nbsp; 1.30&nbsp;&nbsp;&nbsp; 316 csrss<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 181&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 13&nbsp;&nbsp;&nbsp;&nbsp; 5892&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6348&nbsp;&nbsp;&nbsp; 59&nbsp;&nbsp;&nbsp;&nbsp; 9.14&nbsp;&nbsp;&nbsp; 356 csrss<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 306&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 29&nbsp;&nbsp;&nbsp; 13936&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 18312&nbsp;&nbsp; 139&nbsp;&nbsp;&nbsp;&nbsp; 4.36&nbsp;&nbsp; 1300 dfsrs<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 125&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15&nbsp;&nbsp;&nbsp;&nbsp; 2528&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6048&nbsp;&nbsp;&nbsp; 37&nbsp;&nbsp;&nbsp;&nbsp; 0.17&nbsp;&nbsp; 1756 dfssvc<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp; 5159&nbsp;&nbsp;&nbsp; 7329&nbsp;&nbsp;&nbsp; 85052&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 86436&nbsp;&nbsp; 118&nbsp;&nbsp;&nbsp;&nbsp; 1.80&nbsp;&nbsp; 1356 dns<\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: 12px\">What if you wanted to do something else with this information? Perhaps you want to make a change to all of the processes running <\/span><strong style=\"font-size: 12px\">conhost<\/strong><span style=\"font-size: 12px\">. To do this, you&rsquo;d have to filter the list a bit. In a UNIX or Linux shell, you&rsquo;d use a command like Grep, telling it, &ldquo;Look at this text list for me. Keep only those rows where columns 58&ndash;64 contain the characters &lsquo;conhost.&rsquo; Delete all of the other rows.&rdquo; The resulting list would contain only those processes you specified:<\/span><\/p>\n<p class=\"Body\" style=\"padding-left: 30px\" align=\"left\"><span style=\"font-size: 12px\">Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;&nbsp; 1876&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4340&nbsp;&nbsp;&nbsp; 52&nbsp;&nbsp;&nbsp; 11.33&nbsp;&nbsp; 1920 conhost<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 792&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2260&nbsp;&nbsp;&nbsp; 22&nbsp;&nbsp;&nbsp;&nbsp; 0.00&nbsp;&nbsp; 2460 conhost<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 828&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2284&nbsp;&nbsp;&nbsp; 41&nbsp;&nbsp;&nbsp;&nbsp; 0.25&nbsp;&nbsp; 3192 conhost<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p class=\"Body1\" align=\"left\">You&rsquo;d then pipe that text to another command, perhaps telling it to extract the process ID from the list. &ldquo;Go through this and get the characters from columns 52&ndash;56, but drop the first two (header) rows.&rdquo; The result might be this:<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">1920<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">2460<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">3192<\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: 12px\">Finally, you&rsquo;d pipe that text to yet another command, asking it to kill the processes (or whatever else you were trying to do) represented by those ID numbers.<\/span><\/p>\n<p class=\"Body\" align=\"left\">This is, in fact, exactly how UNIX and Linux administrators work. They spend a lot of time learning how to get better at parsing text, using tools like Grep, Awk, and Sed, and becoming proficient in the use of regular expressions. Going through this learning process makes it easier for them to define the text patterns they want their computer to look for. UNIX and Linux folks like programming languages like Perl because those languages contain rich text-parsing and text-manipulation functions. But this text-based approach does present some problems:<\/p>\n<ul>\n<li>You can spend more time messing around with text than doing your real job.<\/li>\n<li>If the output of a command changes (say, moving the ProcessName&nbsp;column&nbsp;to the start of the table), you have to rewrite all of your commands because they&rsquo;re all dependent on things like column positions.<\/li>\n<li>You have to become proficient in languages and tools that parse text&mdash;not because your job involves parsing text, but because parsing text is a means to an end.<\/li>\n<\/ul>\n<p class=\"Body\" align=\"left\">The use of objects in Windows PowerShell helps remove all of that text manipulation overhead. Because objects work like tables in memory, you don&rsquo;t have to tell Windows PowerShell in which text column a piece of information is located. Instead, you tell it the column name, and Windows PowerShell knows exactly where to go to get that data. Regardless of how you arrange the final output on the screen or in a file, the in-memory table is always the same, so you never have to rewrite your commands because a column moved. You spend a lot less time on overhead tasks, and more time focusing on what you want to accomplish.<\/p>\n<p class=\"Body\" style=\"padding-left: 30px\" align=\"left\"><strong>Here is the code for the discount offer today at <\/strong><a href=\"http:\/\/www.manning.com\/\" target=\"_blank\">www.manning.com<\/a>: <strong>scriptw4<\/strong><br \/> Valid for 50% off <a href=\"http:\/\/www.manning.com\/jones3\/\" target=\"_blank\">Learn Windows PowerShell 3 in a Month of Lunches, Second Edition<\/a> and <a href=\"http:\/\/www.manning.com\/helmick\/\" target=\"_blank\">Learn Windows IIS in a Month of Lunches<\/a><br \/><span style=\"font-size: 12px\">Offer valid from April 4, 2013 12:01 AM until April 5 midnight (EST)<\/span><\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PowerShell MVPs, Don Jones and Jeffery Hicks, show the flexibility and the power of Windows PowerShell objects. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for [&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":[64,51,56,65,3,4,45],"class_list":["post-3885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-don-jones","tag-getting-started","tag-guest-blogger","tag-jeffery-hicks","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PowerShell MVPs, Don Jones and Jeffery Hicks, show the flexibility and the power of Windows PowerShell objects. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3885","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=3885"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3885\/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=3885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}