<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Filters</title>
	<atom:link href="http://huddledmasses.org/tag/filters/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>About_Filter considered Harmful</title>
		<link>http://huddledmasses.org/about_filter-considered-harmful/</link>
		<comments>http://huddledmasses.org/about_filter-considered-harmful/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 17:22:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/about_filter-considered-harmful/</guid>
		<description><![CDATA[Someone asked a question in the #PowerShell channel on irc.FreeNode.net today about how to use filters, and pasted this an example like this: filter process-a-m &#123; &#160; &#160;$_.processname -like &#34;[a-m]*&#34; &#125; Get-Process &#124; where &#123;process-a-m&#125; The question was: why doesn&#8217;t this have any output? Well, the answer is: it can&#8217;t have any output. The filter [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked a question in the #PowerShell channel on irc.FreeNode.net today about how to use filters, and pasted this an example like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<br />
<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span>process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<span style="color: #333;">&#125;</span></div>

	<p>The question was: why doesn&#8217;t this have any output? Well, the answer is: it can&#8217;t have any output. The <em>filter</em> outputs true or false for each item passed into it, but the <em>where</em> scriptblock doesn&#8217;t actually pass anything into the filter! Of course we quickly fixed his problem <em>the right way</em> by rewriting the filter to output the items, and getting rid of the where-object call completely &#8230; but when I started wondering where he had gotten the bizarre idea to use a filter like that, his answer was: &#8220;I read about_filter.&#8221;  So I went and looked, and sure enough, the example pasted above is straight out of about_filter.  <strong>And it&#8217;s as wrong as it can possibly be</strong>.</p>

	<p>Let me fix it, and then offer some clarification below.  In <strong>about_filter</strong> there is an example like this: <code>Get-Process | where {$_.processname -like &#34;[a-m]*&#34;}</code> and then a table describing each element of the Where-Object command, and just below it, there is some text about the <strong><em>filter</em></strong> command.  Mentally replace that text with this:<span id="more-445"></span> </p>

	<blockquote>
		<p>You can create a predefined filter by creating a special type of function. When defining the function, you may specify that it is a filter. For the filter script block, you should output items if they pass the test you saw in the preceding example. For instance, the following command creates a filter named process-a-m:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<br />
&nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span></div>
		<p>You can then use the filter name in place of your Where-Object command to retrieve the filtered data, as shown in the following example:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m</div>
		<p>The values returned by the command are the same as they would have been had you specified the conditions using the Where-Object cmdlet as above, but the filter is much easier to reuse on multiple calls, particularly if the conditions were more complicated.</p>
		<p>Of course, a filter function can take parameters, just like any other function, so you could make the range of starting letters more flexible:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>range<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">char</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;a&quot;</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">char</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$m</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;m&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[$a-$m]*&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span></div>
		<p>Now you can use this filter to select between any two letters quite easily:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> <span style="color: #666699; font-weight: bold;">process</span> b j</div>
	</blockquote>

	<h4>Functions vs. Filters</h4>

	<p>To be clear, functions in PowerShell aren&#8217;t like functions in other programming languages, although they <strong>can</strong> just have a body (and be like other functions) they can instead have pipeline syntax.  A function (just like a cmdlet) which is meant to be used in the pipeline has three separate bodies: <strong>begin</strong>, <strong>process</strong>, and <strong>end</strong>, but a filter has only the <em>process</em> part of the body.</p>

	<p>Of course, there are other uses for filters than just limiting which objects come through &#8230; anything that you could do with foreach-object or where-object, you can basically do with a filter in a much more reusable way.  So you don&#8217;t have to write the same thing to the output as what came in the input, you could actually add to it, or remove from it.  A filter for processes could look use Win32_Process to look up their &#8216;owner&#8217; and append it as a property, or could return the process path and name as text &#8230;</p>

	<p>If you&#8217;re new to all of this, let me just present an example function to hopefully clear things up:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> DemoTheCount<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$things</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;things&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">begin</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Let us count $things!&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$counter</span><span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #009900;">&quot;$counter wonderful $things! &nbsp; &nbsp;$_&quot;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">end</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Ha, ha, ha, ha .. $counter wonderful $($_.GetType())es!&quot;</span> <br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>A function without an internal pipeline block is basically like the <strong>begin</strong> block of a pipeline-enabled function: the only things present are the parameters to the function (if there are any).  Then, <strong>process</strong> is called once for each item in the pipeline, with the current item set as the $_ variable. Finally, <strong>end</strong> is called, and you still have the last item in the pipeline present.  The scope persists through each part of the function (so you will see that $counter maintains it&#8217;s value).  The important distinction between this and a function without a <strong>process</strong> body is that a normal function will only be called <em>once</em> when it is in a pipeline &#8212; regardless of how many things are in the pipeline.  If you called DemoTheCount with a list of processes, like <code>Get-Process g* | DemoTheCount</code>, you would get something like this (depending on the processes running on your PC):</p>

	<div class="txt code txt" style="font-family:monospace;">Let us count!<br />
1 wonderful things! &nbsp; &nbsp;System.Diagnostics.Process (gajim)<br />
2 wonderful things! &nbsp; &nbsp;System.Diagnostics.Process (GeoShell)<br />
Ha, ha, ha, ha .. 2 wonderful System.Diagnostics.Process(es)!</div>

	<p>A filter doesn&#8217;t have separate parts, because the <strong>whole</strong> filter is the <strong><em>process</em></strong> part of the function.  If we were to take the process portion of our <code>DemoTheCount</code> function and turn it into a filter, it would only work because the dynamic language would automatically initialize $counter to zero and let you increment it &#8212; anything more complex (or where the type couldn&#8217;t be deduced) would not. When you create a filter using the filter keyword, it&#8217;s pure syntax sugar.  The filter <code>process-a-m</code> defined in my correction to the documentation is <strong>exactly</strong> the same as this function:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> reprocess<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m <br />
<span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span></div>

	<p>You can verify this by creating both of them and then comparing the contents of the resulting functions (<code>(get-content function:\process-a-m) -eq (get-content function:\reprocess-a-m)</code>.  The filter keyword simply creates a function and with the filter body as the <strong>process</strong> body for the function.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/about_filter-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

