{"id":1091,"date":"2014-07-01T00:01:00","date_gmt":"2014-07-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/01\/back-to-the-basics-part-2-learn-about-the-pipeline-and-getters\/"},"modified":"2022-06-10T12:50:33","modified_gmt":"2022-06-10T19:50:33","slug":"back-to-the-basics-part-2-learn-about-the-pipeline-and-getters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/back-to-the-basics-part-2-learn-about-the-pipeline-and-getters\/","title":{"rendered":"Back to the Basics Part 2: Learn about the Pipeline and &#8220;Getters&#8221;"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft PFE, Gary Siepser, talks about the basics of using the Windows PowerShell pipeline and &#8220;getters.&#8221;<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This week, Gary Siepser, a Microsoft PFE is our guest blogger. Yesterday, we posted the first part of his series: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/back-to-the-basics-part-1-learn-about-the-powershell-pipeline\/\" target=\"_blank\" rel=\"noopener\">Back to the Basics Part 1: Learn About the PowerShell Pipeline<\/a>.<\/p>\n<p>In in first post of this series, we looked at what the pipeline is and why you want to use it. We also outlined the common parts of the pipeline by introducing the \u201cgetters, doing something with your data, and outputting your data somewhere. In this post, we\u2019ll focus on the first common portion of the pipeline, the \u201cgetters.\u201d<\/p>\n<p>Pipelines have to start somewhere. Like sending liquid down a real pipeline, we start the trip by putting something into the pipe. We need some data. In Windows PowerShell, this data is actually structured data in objects. Right now the objects aren\u2019t that important for starting a pipe, but the sooner you start thinking &#8220;objects,\u201d the more power you\u2019ll put into the shell. The first part of the pipeline is going to be some kind of command that outputs the data you want to work with.<\/p>\n<p>In Part 1, we investigated using the <strong>Show-Command<\/strong> pane in the ISE to find commands. Using that technique, you can find potentially thousands of sources of your data. Most of the time, the command you will use starts with the verb <strong>Get-<\/strong>. This is not surprising because this is the very reason the verb exists. In the Windows PowerShell SDK, the exact description is \u201cSpecifies an action that retrieves a resource.\u201d For more information about verb choices, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms714428(v=vs.85).aspx\" target=\"_blank\" rel=\"noopener\">Approved Verbs for Windows PowerShell Commands<\/a>.<\/p>\n<p>The following list is completely overwhelming and long, but take a look and appreciate how many <strong>Get-<\/strong> commands I see on my laptop (which is still a mere subset of what is possible on a Windows Server operating system with lots of roles and features).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Large-Get-Commands.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Large-Get-Commands.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If you made it this far, you deserve to know that there are 342 commands in this listed. I know that was a lot to look at, but I can\u2019t stress enough how much coverage there is now with Windows PowerShell and how many things you can work with by using these commands.<\/p>\n<p>Although this post is about the \u201cgetters,\u201d and a majority of the time you are going to start your pipeline with one of these commands, there are also other great ways to start the pipeline.<\/p>\n<p>Pipelines can start with external DOS commands, imports from other file formats (such as CSV and XML), or a variable. The bottom line is that we need some data\u2014some objects from somewhere. Depending on which method you choose can greatly affect how you continue with the pipeline later. Let\u2019s take a look at each of these techniques.<\/p>\n<h4>DOS commands \u00a0\u00a0<\/h4>\n<p>Trusty old external DOS commands seem like they will never die (until they are literally removed from the operating sysem, I suppose). I always think to the common staples like ping.exe or ipconfig.exe. Now we have native Windows PowerShell cmdlets for both of those, but there are still some oldies-but-goodies out there. Think of aging stuff like nbtstat.exe (that is \u201cNetBIOS over TCP statistics\u201d for the old-timers).<\/p>\n<p>When you run these external commands, you get back text. Although technically these lines of text are string objects in Windows PowerShell, they are in fact pretty simple objects that are unstructured. Working with this data in Windows PowerShell is not very easy. Parsing text often follows external commands. Even with the challenges, you can get done what you need to. Native Windows PowerShell commands are nearly always more desirable than external commands, especially when getting data.<\/p>\n<h4>Other file formats \u00a0\u00a0<\/h4>\n<p>Importing data from other file formats is also a neat way to start the pipeline. It\u2019s very common as an IT admin to move data around between systems, and common formats (like CSV) are a great medium to do so.<\/p>\n<p><strong>Import-CSV<\/strong> is a cmdlet in Windows PowerShell that does a fantastic job. It\u2019s smart enough to look at the header row to determine what properties each object will get, and then turn each data row into one of those objects. The best part is that those objects (structured data), will travel down the pipeline and be usable for all the manipulation and outputting we want to do later in the pipeline. We\u2019ll cover that in the next two posts in this series.<\/p>\n<p><strong>Import-CSV<\/strong> is shockingly easy to use. Take a look at the data in the following CSV file:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-1.png\" alt=\"Image of file\" title=\"Image of file\" \/><\/a><\/p>\n<p>Now take a look at how nicely <strong>Import-CSV<\/strong> brings in that data:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It\u2019s that simple. In the next post when we talk about doing things with your data, you\u2019ll see how to sort it. For now, here is an early peek:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>You can see how easy it is. For more information, check out the Help file for <strong>Import-CSV<\/strong>. CSV is certainly not the only format you can import from, but it\u2019s a good start. Look for other cmdlets that can import data from other formats.<\/p>\n<h4>Variables\u00a0\u00a0<\/h4>\n<p>You can also start a pipeline with data you have already stored in a variable. A variable is simply a place to store your stuff. You\u2019ll recognize variables because their name is usually proceeded by a dollar sign (for example, <strong>$Services<\/strong>). I don\u2019t want to get deeply into variables, but here is a quick look at a basic way to save something into a variable that you can use to start your pipeline:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-1-14-4.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In this example, you can see that we stored the results of the <strong>Get-Service<\/strong> command into a variable called <strong>$MyServices<\/strong>. On the next line, we used this variable to start the pipeline. This really doesn\u2019t end up being any different than using <strong>Get-Service<\/strong> to start the pipeline. Where using a variable is great is if I want to use this stored data more than once. I don\u2019t need to get it all again. Why duplicate the effort when I can get it once and use the results over and over again.<\/p>\n<p>By looking at these three techniques, you can see that starting a pipeline is really flexible. Always remember about searching for commands by using the <strong>Show-Command<\/strong> pane in the ISE and reading the Help to understand how to use them. In the next two posts, we will look at manipulating your data in the pipeline.<\/p>\n<p>~Gary<\/p>\n<p>Thanks Gary. Join us tomorrow for Part 3 of our Back to the Basics series.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PFE, Gary Siepser, talks about the basics of using the Windows PowerShell pipeline and &#8220;getters.&#8221; Microsoft Scripting Guy, Ed Wilson, is here. This week, Gary Siepser, a Microsoft PFE is our guest blogger. Yesterday, we posted the first part of his series: Back to the Basics Part 1: Learn About the PowerShell Pipeline. [&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":[317,51,56,149,3,4,45],"class_list":["post-1091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-gary-siepser","tag-getting-started","tag-guest-blogger","tag-pipeline","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PFE, Gary Siepser, talks about the basics of using the Windows PowerShell pipeline and &#8220;getters.&#8221; Microsoft Scripting Guy, Ed Wilson, is here. This week, Gary Siepser, a Microsoft PFE is our guest blogger. Yesterday, we posted the first part of his series: Back to the Basics Part 1: Learn About the PowerShell Pipeline. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1091","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=1091"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1091\/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=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}