{"id":16051,"date":"2010-12-31T00:01:00","date_gmt":"2010-12-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/12\/31\/write-powershell-functions-that-accept-pipelined-input\/"},"modified":"2010-12-31T00:01:00","modified_gmt":"2010-12-31T00:01:00","slug":"write-powershell-functions-that-accept-pipelined-input","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/write-powershell-functions-that-accept-pipelined-input\/","title":{"rendered":"Write PowerShell Functions That Accept Pipelined Input"},"content":{"rendered":"<p><span style=\"font-size:10.0pt\"><\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><\/span><\/p>\n<p><b><span style=\"font-size:10.0pt\">Summary:<\/span><\/b><span style=\"font-size:10.0pt\"> Learn how to write Windows PowerShell functions that accept pipelined input.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! I really like the way that some Windows PowerShell cmdlets enable me to <\/span><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\"><span style=\"font-size:10.0pt\">pipeline<\/span><\/a><span style=\"font-size:10.0pt\"> things to it. I do not understand why some Windows PowerShell cmdlets do not allow me to pipeline stuff to it, or perhaps I am doing it wrong. Anyway, my question is as follows: How can I write a function that will accept input from the pipeline? I think it would add a lot of additional capability to my functions, and enable them to behave more the way that Windows PowerShell, in general, tends to behave. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&#8212; ES<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello ES, Microsoft Scripting Guy Ed Wilson here. One of the weird things about living in Charlotte, <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/North_Carolina\"><span style=\"font-size:10.0pt\">North Carolina<\/span><\/a><span style=\"font-size:10.0pt\"> is that the winters are generally very mild. Weird, some of my Canadian friends say, weird. After having lived in the Deep South for several years, I do, in fact, miss Winter. I imagine I would get tired of shoveling snow, scraping ice, and playing &ldquo;bumper cars&rdquo; on the freeway if I had to do it year after year, but as an occasional respite from the normal southern cloudy, cold, and drizzling rain that is the seasonal norm around here, I think I would like to take my chances. In fact, the time that the <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\"><span style=\"font-size:10.0pt\">Scripting Wife<\/span><\/a><span style=\"font-size:10.0pt\"> and I spent in Quebec, Canada a few years ago when I was teaching Windows PowerShell workshops to Microsoft Premier Customers was our favorite winter. The following image is a picture I took of Quebec from an ice-breaking ferryboat as it crossed the <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/Saint_Lawrence_River\"><span style=\"font-size:10.0pt\">St. Lawrence River<\/span><\/a><span style=\"font-size:10.0pt\">. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6406.HSG-12-31-10-01.jpg\" border=\"0\" \/><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">Of course, one&rsquo;s perspective on winter depends a great deal upon where one is used to residing. My friends in Sydney, <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/Australia\"><span style=\"font-size:10.0pt\">Australia<\/span><\/a><span style=\"font-size:10.0pt\"> are telling me on Facebook that it is 81 degrees Fahrenheit today (27 degrees Celsius). <\/span><\/p>\n<p class=\"Readeraidonly\" style=\"margin-left:0in\"><span style=\"font-size:10.0pt\">One of the most useful things I ever did, Windows PowerShell wise, was my conversion module. I use it almost every day to convert temperatures from Fahrenheit to Celsius and vice versa. Of course, it also converts distance, and volume in addition to other things. I developed it during the <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/modules\/weekend+scripter\/\"><span style=\"font-size:10.0pt\">first six Weekend Scripter articles<\/span><\/a><span style=\"font-size:10.0pt\">. If you only want to see the final product, check out <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/02\/21\/hey-scripting-guy-february-21-2010.aspx\"><span style=\"font-size:10.0pt\">version six of the module<\/span><\/a><span style=\"font-size:10.0pt\">. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">ES, there are several ways that you can provide input values to a function. In today&rsquo;s post, I will examine four ways. Two of the methods do not use the pipeline, and two of the methods do use the pipeline. As a quick review, using the pipeline in Windows PowerShell is a technique where the output of one command is streamed directly into the input of another command. A nice example of this would be to read a text file that contains a listing of directories (as seen in the following figure) and pipeline the results of reading the text file to the <b>Get-ChildItem<\/b> Windows PowerShell cmdlet (<b>Get-ChildItem<\/b> has the alias of <i>dir<\/i>). <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">H<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3603.HSG-12-31-10-02.jpg\" border=\"0\" \/><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">The command to perform this pipeline operation appears here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">P<span style=\"font-family: courier new,courier\">S C:\\&gt; Get-Content C:\\fso\\Folders.txt | Get-ChildItem<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">If I want to configure my function to accept pipelined input, I can use the process directive. This has the advantage of simplicity, and in addition, it makes the script really easy to read. This is seen here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size:10.0pt\">F<span style=\"font-family: courier new,courier\">unction add-oneD<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">{<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>Process { $_ + 1 }<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">} #end function add-oneD<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">The <b>add-oneD<\/b> function does not define any input parameters. Instead, it uses the <i>$_ automatic<\/i> variable to accept the current value that is on the pipeline. To call this function, I can pipeline a group of numbers directly to the <b>add-oneD<\/b> function. This is seen here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">1<span style=\"font-family: courier new,courier\">..5000000 | add-oneD<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To check the performance of the function, I use the <b>Measure-Command<\/b> Windows PowerShell cmdlet. This is seen here, along with the associated output.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">M<span style=\"font-family: courier new,courier\">easure-command -Expression { 1..5000000 | add-oneD }<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Days<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Hours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Minutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Seconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 35<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Milliseconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 17<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Ticks<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 350172660<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalDays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.000405292430555556<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalHours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.00972701833333333<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMinutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.5836211<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalSeconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 35.017266<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMilliseconds : 35017.266<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">It took around 35 seconds on my computer to add five million numbers &ndash; not too bad. But, perhaps I can make it perform a little better. The second way to configure the function to accept direct pipeline input is to use the <b>$input<\/b> variable. As seen here, the <b>add-oneC<\/b> function uses the <\/span><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/05\/05\/how-can-i-loop-through-collections-with-windows-powershell.aspx\"><i><span style=\"font-size:10.0pt\">Foreach<\/span><\/i><\/a><span style=\"font-size:10.0pt\"> command to iterate through the <b>$input<\/b> automatic variable. When anything is piped to a function, the stream will be contained in the <b>$input<\/b> variable. The <b>add-oneC<\/b> function appears here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size:10.0pt\">F<span style=\"font-family: courier new,courier\">unction add-oneC<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">{<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>foreach($a in $input)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp; <\/span>{ $a + 1 }<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">} #end function add-oneC<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To call the <b>add-oneC<\/b> function, I use the same kind of command that I used when calling the <b>add-oneD<\/b> function. I pipeline the numbers directly to the function. This command appears here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">1<span style=\"font-family: courier new,courier\">..5000000 | add-oneC<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To measure the performance of the new function, I use the <b>Measure-Command<\/b> Windows PowerShell cmdlet as seen here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">M<span style=\"font-family: courier new,courier\">easure-command -Expression { 1..5000000 | add-oneC }<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">The performance increase is remarkable. As seen here, the new function only takes a little more than fifteen seconds to add the five million numbers. <\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">D<span style=\"font-family: courier new,courier\">ays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Hours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Minutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Seconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 15<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Milliseconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 455<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Ticks<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 154554599<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalDays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.000178882637731481<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalHours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.00429318330555556<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMinutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.257590998333333<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalSeconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 15.4554599<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMilliseconds : 15455.4599<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">In a quest for performance, suppose I create the array of numbers, and pass the array directly to the function. Then inside the function I pipeline the items. Will this be faster? Here is the <b>add-oneB <\/b>function.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size:10.0pt\">F<span style=\"font-family: courier new,courier\">unction add-oneB<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">{<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>Param ($a)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>$a | foreach-object { $_ + 1 }<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">} #end add-oneB<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To call this function, I first have to create an array with five million numbers in it, and then pass that array to the function. Here is the code that does that.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">$<span style=\"font-family: courier new,courier\">a = 1..5000000<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">add-oneB -a $a<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">When I call the <b>Measure-Command<\/b> to check the performance, the results are a bit disappointing. Here is the command, and the results.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">$<span style=\"font-family: courier new,courier\">a = 1..5000000<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Measure-command -Expression { add-oneB -a $a }<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Days<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Hours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Minutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 5<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Seconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 20<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Milliseconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 372<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Ticks<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 3203725668<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalDays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.00370801581944444<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalHours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.0889923796666667<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMinutes <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>: 5.33954278<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalSeconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 320.3725668<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMilliseconds : 320372.5668<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">Three hundred and twenty seconds; <\/span><a href=\"http:\/\/en.wikipedia.org\/wiki\/Dude\"><span style=\"font-size:10.0pt\">Dude<\/span><\/a><span style=\"font-size:10.0pt\"> (or Dudette) that is more than five minutes &ndash; clearly this is not a very good approach to the problem. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">I decided to try one more approach, creating an array, and passing it to the function. Here is the <b>add-oneA<\/b> function.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size:10.0pt\">F<span style=\"font-family: courier new,courier\">unction add-oneA<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">{<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>Param ($a)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;<\/span>foreach($b in $a)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp; <\/span>{ $b + 1 }<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"margin-left: .5in;background: transparent\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">} #end function add-oneA<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">Again, I have to create the array with five million numbers in it, and pass it to the function. This is seen here.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">$<span style=\"font-family: courier new,courier\">a = 1..5000000<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">add-oneA -a $a<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To check the performance of the new function, I again call on the <b>Measure-Command<\/b> Windows PowerShell cmdlet as seen here. <\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">$<span style=\"font-family: courier new,courier\">a = 1..5000000<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Measure-command -Expression { add-oneA -a $a }<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">The results this time are impressive (as seen here,) the function processed five million numbers in a little less than eight seconds.<\/span><\/p>\n<blockquote>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size:10.0pt\">D<span style=\"font-family: courier new,courier\">ays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Hours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Minutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Seconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Milliseconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 964<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Ticks<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 79640050<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalDays<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 9.21759837962963E-05<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalHours<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.00221222361111111<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMinutes<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 0.132733416666667<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalSeconds<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7.964005<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">TotalMilliseconds : 7964.005<\/span><\/span><\/p>\n<\/blockquote>\n<p><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">ES, your design decision therefore, boils down to how you anticipate users using your function. If you think they will be pipelining information directly to the function, you may want to consider using the <b>$input<\/b> variable as seen in function <b>add-oneC<\/b>. If storing in an array, and then passing to the function will work for you, then using the <i>foreach<\/i> statement inside the function (as opposed to pipelining the array) would seem to be the better approach. However, in all things, you should test as your performance may vary, as may the performance with different types of data. You should be very careful when making decisions based on mere milliseconds of difference as the <b>Measure-Command<\/b> Windows PowerShell cmdlet really is not that precise (that is why I decided to use 5,000,000 numbers to ensure the commands would take significant time and would therefore make the differences more appreciable). <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">For more information about performance testing of changes to Windows PowerShell scripts, refer to the &ldquo;<\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/07\/23\/hey-scripting-guy-how-can-i-test-the-efficacy-of-my-script-modifications.aspx\"><span style=\"font-size:10.0pt\">How can I test the efficacy of my script modifications<\/span><\/a><span style=\"font-size:10.0pt\">&rdquo; Hey, Scripting Guy! blog post. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">ES, that is all there is to using the Windows PowerShell pipeline. This concludes script design week. Join me tomorrow as I talk about how to work with music files. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">I invite you to follow me on <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"font-size:10.0pt\">Twitter<\/span><\/a><span style=\"font-size:10.0pt\"> or <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-size:10.0pt\">Facebook<\/span><\/a><span style=\"font-size:10.0pt\">. If you have any questions, send email to me at <\/span><a target=\"_blank\" href=\"mailto:scripter@microsoft.com\"><span style=\"font-size:10.0pt\">scripter@microsoft.com<\/span><\/a><span style=\"font-size:10.0pt\"> or post them on the <\/span><a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\"><span style=\"font-size:10.0pt\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-size:10.0pt\">. See you tomorrow. Until then, peace.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><b><span style=\"font-size:10.0pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to write Windows PowerShell functions that accept pipelined input. &nbsp; Hey, Scripting Guy! I really like the way that some Windows PowerShell cmdlets enable me to pipeline things to it. I do not understand why some Windows PowerShell cmdlets do not allow me to pipeline stuff to it, or perhaps I am [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,3,4,45],"class_list":["post-16051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to write Windows PowerShell functions that accept pipelined input. &nbsp; Hey, Scripting Guy! I really like the way that some Windows PowerShell cmdlets enable me to pipeline things to it. I do not understand why some Windows PowerShell cmdlets do not allow me to pipeline stuff to it, or perhaps I am [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16051","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16051\/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=16051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}