{"id":2496,"date":"2013-12-02T00:01:00","date_gmt":"2013-12-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/02\/getting-started-with-powershell-the-pipeline\/"},"modified":"2013-12-02T00:01:00","modified_gmt":"2013-12-02T00:01:00","slug":"getting-started-with-powershell-the-pipeline","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-powershell-the-pipeline\/","title":{"rendered":"Getting Started with PowerShell: The Pipeline"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, shares an excerpt from his new Windows PowerShell book.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today, I have an excerpt from my recently published book,&nbsp;<a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-First-Steps\/dp\/0735681007\/ref=la_B001ILFMZ8_1_2_title_1_pap?s=books&amp;ie=UTF8&amp;qid=1383673887&amp;sr=1-2\" target=\"_blank\">Windows PowerShell 3.0 First Steps<\/a>. This book is published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5852.First%20Steps%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5852.First%20Steps%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p>The Windows PowerShell pipeline takes the output from one command, and sends it as input to another command. By using the pipeline, you are able to do things such as find all computers in one specific location and restart them. This entails two commands:<\/p>\n<ol>\n<li>Find all the computers in a specific location<\/li>\n<li>Restart each of the computers<\/li>\n<\/ol>\n<p>Passing the objects from one command to a new command makes Windows PowerShell easy to use inside the console because you do not have to stop to parse the output from the first command before taking action with a second command.<\/p>\n<p>Windows PowerShell passes objects down the pipeline. This is one way that Windows PowerShell becomes very efficient: It takes an object (or group of objects) from the results of running one command, and it passes those objects to the input of another command.<\/p>\n<p>By using the Windows PowerShell pipeline, it is not necessary to store the results of one command into a variable, and then call a method on that object to perform an action. For example, the following command disables all network adapters on my Windows&nbsp;8 laptop.<\/p>\n<p class=\"Readeraidwithicon\"><strong>Note<\/strong> &nbsp;Windows PowerShell honors the Windows security policy. Therefore, to disable a network adapter, you must run Windows PowerShell with Admin rights.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-NetAdapter | Disable-NetAdapter<\/p>\n<p>In addition to disabling all network adapters, you can enable them. To do this, use the <strong>Get-NetAdapter<\/strong> cmdlet and pipe the results to the <strong>Enable-NetAdapter<\/strong> cmdlet, as shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-NetAdapter | Enable-NetAdapter<\/p>\n<p>If you want to start all of the virtual machines on a computer running Windows 8 (or on a server running Windows Server&nbsp;2012), use the <strong>Get-VM<\/strong> cmdlet and pipe the resulting virtual machine objects to the <strong>Start-VM<\/strong> cmdlet:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-VM | Start-VM<\/p>\n<p>To shut down all of the virtual machines, use the <strong>Get-VM<\/strong> cmdlet and pipe the resulting virtual machine objects to the <strong>Stop-VM<\/strong> cmdlet:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-VM | Stop-VM<\/p>\n<p>In each of the previous commands, an object (or group of objects) that results from one command is piped to another cmdlet for further action.<\/p>\n<h2>Sorting output from a cmdlet<\/h2>\n<p>The <strong>Get-Process<\/strong> cmdlet generates a nice table view of process information to the Windows PowerShell console. The default view appears in ascending alphabetical order by process name. This view is useful for helping find specific process information; but it hides important details, such as which process uses the least, or the most, virtual memory.<\/p>\n<p>To sort the output from the process table, pipe the results from the <strong>Get-Process<\/strong> cmdlet to the <strong>Sort-Object<\/strong> cmdlet and supply the <strong>&ndash;Property<\/strong> parameter to the property upon which to sort. The default sort order is ascending (that is, the smallest number appears at the top of the list).<\/p>\n<p>The following command sorts the process output by the amount of virtual memory that is used by each process. The processes that consume the least amount of virtual memory will appear at the top of the list.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-Process | Sort-Object -Property VM<\/p>\n<p>If you are interested in which processes consume the most virtual memory, you may want to reverse the default sort order. To do this, use the <strong>&ndash;Descending<\/strong> switch parameter. This command is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-Process | Sort-Object -Property VM &ndash;Descending<\/p>\n<p>The command to produce the sorted list of virtual memory processes, and the associated output from the command are shown in the following image:<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4186.12-2-13-1.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4186.12-2-13-1.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p class=\"Num-Caption\">It is possible to shorten the length of Windows PowerShell commands that use the <strong>Sort-Object<\/strong> cmdlet. The command <strong>Sort<\/strong> is an alias for the <strong>Sort-Object<\/strong> cmdlet. A cmdlet alias is a shortened form of the cmdlet name that Windows PowerShell recognizes as a substitute for the complete cmdlet name. Some aliases are easily recognizable (such as <strong>Sort<\/strong> for <strong>Sort-Object<\/strong> or <strong>Select<\/strong><em> <\/em>for <strong>Select-Object<\/strong>). Other aliases must be learned (such as <strong><em>?<\/em><\/strong> for the <strong>Where-Object<\/strong>&mdash;most Windows users expect <strong>? <\/strong>to be an alias for the <strong>Get-Help<\/strong> cmdlet).&nbsp;<\/p>\n<p>In addition to using an alias for the <strong>Sort-Object<\/strong> cmdlet name, the <strong>&ndash;Property<\/strong> parameter is the default parameter that the cmdlet utilizes; therefore, it can be left out of the command. The following command uses the shortened syntax to produce a list of services by status.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-Service | sort status<\/p>\n<p>It is possible to sort on more than one property. You need to be careful doing this because at times it is not possible to sort additional properties. With the <strong>Service<\/strong> cmdlets, a multiple sort makes sense because there are two broad categories of status: Running and Stopped. It therefore makes sense to attempt to organize the output further to facilitate finding particular stopped or running services.<\/p>\n<p>One way to facilitate finding services is to alphabetically sort the <strong>DisplayName<\/strong><em> <\/em>property of each service. The following script sorts the service objects obtained via the <strong>Get-Service<\/strong> cmdlet by the status, and then by the display name<em> <\/em>from within the status. The output appears in descending order instead of the default ascending list order.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-Service | sort status, displayname &ndash;Descending<\/p>\n<p>The following image shows the command to sort services by status and display name and the output from the command:<\/p>\n<p class=\"Fig-Graphic\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0028.12-2-13-2.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0028.12-2-13-2.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Join me tomorrow when I will have another excerpt from my Windows PowerShell 3.0 First Steps book.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shares an excerpt from his new Windows PowerShell book. Microsoft Scripting Guy, Ed Wilson, is here. Today, I have an excerpt from my recently published book,&nbsp;Windows PowerShell 3.0 First Steps. This book is published by Microsoft Press. The Windows PowerShell pipeline takes the output from one command, and sends [&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-2496","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, shares an excerpt from his new Windows PowerShell book. Microsoft Scripting Guy, Ed Wilson, is here. Today, I have an excerpt from my recently published book,&nbsp;Windows PowerShell 3.0 First Steps. This book is published by Microsoft Press. The Windows PowerShell pipeline takes the output from one command, and sends [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2496","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=2496"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2496\/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=2496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}