<?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; CommandType</title>
	<atom:link href="http://huddledmasses.org/tag/commandtype/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>PowerShell Power User Tips: Get-Command precedence</title>
		<link>http://huddledmasses.org/powershell-power-user-tips-get-command-precedence/</link>
		<comments>http://huddledmasses.org/powershell-power-user-tips-get-command-precedence/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 21:46:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CommandType]]></category>
		<category><![CDATA[Ordering]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PUT]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-power-user-tips-get-command-precedence/</guid>
		<description><![CDATA[This is the first in what I hope will be an occasional series of tips for PowerShell users: short posts which don&#8217;t intend to give guidance, but merely a tip on a feature you may not be aware of, or maybe even answers to some of the recurring questions that come up in #PowerShell. We&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[	<p>This is the first in what I hope will be an occasional series of tips for PowerShell users: short posts which don&#8217;t intend to give guidance, but merely a tip on a feature you may not be aware of, or maybe even answers to some of the recurring questions that come up in #PowerShell.  We&#8217;ll see how this goes &#8230; </p>

	<h5>My first tip to power users is to remember the priority which PowerShell assigns to things by default:</h5>

	<ol>
		<li>Alias</li>
		<li>Function (and Filter)</li>
		<li>Cmdlet</li>
		<li>ExternalScript</li>
		<li>Application</li>
	</ol>
	<ol>
		<li>Files with associations</li>
	</ol>

	<p>This means that using an Alias, you can override <em>anything</em> (and since you can&#8217;t have multiple aliases with the same name, that makes aliases the ultimate way to disambiguate commands).  </p>

	<p>Functions and filters (there&#8217;s really <a href="http://groups.google.com/group/microsoft.public.windows.powershell/browse_thread/thread/dc8b8cd1292b3878/f2f63066660e649a#f2f63066660e649a">no such thing as a filter</a>) come before cmdlets (yes, even built-in cmdlets) and scripts (that is, your .ps1 files which are in your path, and are called &#8220;ExternalScripts&#8221; by PowerShell) come before applications, which come before scripts written in other languages, like .vbs or .bat or .cmd (even though they&#8217;re all shown as &#8220;Application&#8221; type commands, PowerShell prioritizes apps over executable scripts which are associated with an engine).</p>

	<p>Incidentally, I&#8217;m not <em>actually</em> sure what a &#8220;Script&#8221; is (it&#8217;s a CommandType for Get-Command, but there aren&#8217;t any on my PCs) if anyone can run <code>get-command -type &#34;Script&#34;</code> and get something output, please let me know what it was.</p>

	<p>It&#8217;s important to understand that this precedence order is <em>not</em> what you get if you run <code>Get-Command</code> (Get-Command merely orders everything alphabetically by name). In fact, as far as I know, PowerShell doesn&#8217;t give you a way to get a list of commands in the order that they would execute, nor does it specify any way of determining that order in the documentation. If you would like to see the proper order, you could use the following &#8220;filter&#8221; function, and sort by <code>Order</code> bearing in mind that it really only works precisely when you type the full command without wildcards.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">filter</span> which <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Order</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;luimcxp&quot;</span>.<span style="color: #003366;">IndexOf</span><span style="color: #333;">&#40;</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">CommandType</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># So then you can pipe through which, and sort by order:</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Command</span></span> SomeCommand <span style="color: #66cc66;">|</span> which <span style="color: #66cc66;">|</span> <span style="color: #660033;">sort</span> order<br />
&nbsp;</div>

	<p>Oh, by the way, in case it&#8217;s not obvious &#8230; &#8220;luimcxp&#8221; is the second letter of each &#8220;CommandType&#8221; (which is unique in each one) in the order that I think they belong in &#8230; so <code>IndexOf( $($_.CommandType)[1] )</code> gives us the correct ordering for the command in <code>$_</code>.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-power-user-tips-get-command-precedence/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

