{"id":2615,"date":"2013-11-06T00:01:00","date_gmt":"2013-11-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/11\/06\/a-powershell-object-lesson-part-2\/"},"modified":"2013-11-06T00:01:00","modified_gmt":"2013-11-06T00:01:00","slug":"a-powershell-object-lesson-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-powershell-object-lesson-part-2\/","title":{"rendered":"A PowerShell Object Lesson: Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about the importance of returning objects to ease administrative work.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping a magnificent cup of Oolong green tea. I added a spoonful of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jasmine\" target=\"_blank\">jasmine buds<\/a> to my teapot, in addition to a half cinnamon stick and a spoonful of lemon grass. The result is perfect. It goes well with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Macadamia\" target=\"_blank\">macadamia nuts<\/a> I am munching on right now.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;&nbsp;<\/strong>This is the second part of a three part series that talk about Windows PowerShell objects. In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/11\/05\/a-powershell-object-lesson-part-one.aspx\" target=\"_blank\">A PowerShell Object Lesson: Part 1<\/a>, I talked about the advantage of using Windows PowerShell objects, and how they are helpful to the scripter.<\/p>\n<h2>I got it through the pipeline<\/h2>\n<p>The next really cool thing about objects is that I can send them down the pipeline to other commands. The other commands might do things or format things. For example, a common way that people who come from other scripting languages write scripts is to &ldquo;echo&rdquo; everything. When they find the <strong>Write-Host<\/strong> cmdlet, they immediately begin to select the properties they want to display. An example of such a script is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.HSG-11-6-13-01.png\"><img decoding=\"async\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7853.HSG-11-6-13-01.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>In addition to wasting a lot of time selecting properties, it takes a long time to write all that code. I am actually stepping back in time in terms of usefulness and productivity. The default output from <strong>Get-Process <\/strong>is actually better, in my opinion&mdash;and as will be seen in a bit, it is more useful. Here is the default output from <strong>Get-Process<\/strong>:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7356.HSG-11-6-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7356.HSG-11-6-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>If I send the output to the <strong>Get-Member<\/strong> cmdlet, I can see that there are more properties available than just the default displayed here. In fact, there are a number of methods as shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3566.HSG-11-6-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3566.HSG-11-6-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The cool thing about methods is that methods do things. After I use the <strong>Get-Member<\/strong> cmdlet, the first thing that appears at the top of the screen is the <strong>TypeName<\/strong> line. It tells me that I have an instance of the System.Diagnostics.Process object. If I do not know what that is, I can look up that exact phrase on MSDN, and it will tell me everything about the object&mdash;including how to use the various methods that appear in the output.<\/p>\n<p>The cool thing about Windows PowerShell and objects is that I do not need to dive into the .NET Framework or MSDN to be able to do some things. For example, my <strong>Process<\/strong> object has the following cmdlets that automatically work:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Command -Noun process<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">CommandType&nbsp;&nbs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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Microsoft.Powe&#8230;<\/p>\n<p>For example, if I want to stop the Notepad process, I can open MSDN and figure out how to call the <strong>Kill()<\/strong><em> <\/em>method, or I can simply try the <strong>Stop-Process<\/strong> cmdlet. When I take the process object that comes from <strong>Get-Process<\/strong> and send it to the <strong>Stop-Process<\/strong> object, it automatically stops the processes. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Process notepad | Stop-Process<\/p>\n<p>The following image illustrates the command, and then shows that <strong>Get-Process<\/strong> can no longer find any Notepad processes:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8737.HSG-11-6-13-04.png\"><img decoding=\"async\" title=\"Image of message\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8737.HSG-11-6-13-04.png\" alt=\"Image of message\" \/><\/a><\/p>\n<p>Without using the pipeline, I would need to call the <strong>Kill<\/strong><em> <\/em>method. It is not difficult, but it is another step. Here is an example:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $process = Get-Process notepad<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $process.Kill()<\/p>\n<p>Join me tomorrow when I will talk more about working with objects.<\/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 Scripting Guy, Ed Wilson, talks about the importance of returning objects to ease administrative work. Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping a magnificent cup of Oolong green tea. I added a spoonful of jasmine buds to my teapot, in addition to a half cinnamon stick and a [&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":[51,3,4,45],"class_list":["post-2615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about the importance of returning objects to ease administrative work. Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping a magnificent cup of Oolong green tea. I added a spoonful of jasmine buds to my teapot, in addition to a half cinnamon stick and a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2615","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=2615"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2615\/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=2615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}