<?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; PUT</title>
	<atom:link href="http://joelbennett.net/tag/put/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</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='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell Power User Tips: Bash-style &#8220;alias&#8221; command.</title>
		<link>http://joelbennett.net/powershell-power-user-tips-bash-style-alias-command/</link>
		<comments>http://joelbennett.net/powershell-power-user-tips-bash-style-alias-command/#comments</comments>
		<pubDate>Sat, 03 May 2008 04:39:04 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[PowerUser]]></category>
		<category><![CDATA[PUT]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-power-user-tips-bash-style-alias-command/</guid>
		<description><![CDATA[I keep hearing from new users who are used to bash-style aliases, how frustrating it is not to be able to create aliases with parameters, the way you can in bash &#8230; I&#8217;m going to show you how to make the &#8220;alias&#8221; command work roughly the way it does in bash, but first, let me [...]]]></description>
			<content:encoded><![CDATA[	<p>I keep hearing from new users who are used to bash-style aliases, how frustrating it is not to be able to create aliases with parameters, the way you can in bash &#8230;</p>

	<p>I&#8217;m going to show you how to make the &#8220;alias&#8221; command work roughly the way it does in bash, but first, let me clear this up:</p>

	<table>
		<tr>
			<th>Bash or Csh </th>
			<th>PowerShell </th>
		</tr>
		<tr>
			<td> script </td>
			<td> script </td>
		</tr>
		<tr>
			<td> <a href="http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+alias">alias</a>, <a href="http://www.faqs.org/docs/bashman/bashref_22.html">shell functions</a> </td>
			<td> function </td>
		</tr>
		<tr>
			<td> ... </td>
			<td> alias </td>
		</tr>
	</table>

	<p>In Bash, the recommendation is that &#8220;for almost every purpose, shell functions are preferred over aliases&#8221; ... and that recommendation is even stronger for PowerShell. The PowerShell &#8220;alias&#8221; is a true <em>alias</em> &#8212; it&#8217;s <em>just another name</em> for a command, script, function, etc. and it doesn&#8217;t support passing parameters or making mini-scripts at all. </p>

	<p>Usually, an alias serves to give you a name you can remember, but sometimes it&#8217;s just to shorten the name, .  Another use for them is to let you specify the default cmdlet &#8212; if you have two cmdlets (or script functions) with the same name, you can use an alias to override which one is executed by default.</p>

	<p>The PowerShell function can do everything a bash alias can do (and everything a function or script can do, but this isn&#8217;t a tip about that, it&#8217;s a tip about making an alias  <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  and unalias command that works the way you expect it to).  So &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Aliases.ps1 -- to be dot-sourced from your profile</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">Version</span>.<span style="color: #003366;">Major</span> <span style="color: #000066;">-ge</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> script:<span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Aliases</span></span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$line</span><span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSParser</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Tokenize</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$line</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &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; font-weight: bold;">Type</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Command&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cmd</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span>which <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &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;">$cmd</span>.<span style="color: #003366;">CommandType</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Alias&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$line</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$line</span>.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StartColumn</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Length</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Insert</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StartColumn</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>, <span style="color: #660033; font-weight: bold;">$cmd</span>.<span style="color: #003366;">Definition</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$line</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> alias <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># pull together all the args and then split on =</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$alias</span>,<span style="color: #660033; font-weight: bold;">$cmd</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span>::<span style="color: #333399; font-weight: bold; font-style: italic;">join</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span>,<span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;=&quot;</span>,<span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">Version</span>.<span style="color: #003366;">Major</span> <span style="color: #000066;">-ge</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cmd</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Aliases</span></span> <span style="color: #660033; font-weight: bold;">$cmd</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Item</span></span> <span style="color: #000066;">-Path</span> <span style="color: #666699; font-weight: bold;">function</span>: <span style="color: #000066;">-Name</span> <span style="color: #009900;">&quot;Global:Alias$Alias&quot;</span> <span style="color: #000066;">-Options</span> <span style="color: #009900;">&quot;AllScope&quot;</span> <span style="color: #000066;">-Value</span> @<span style="color: #009900;">&quot;<br />
Invoke-Expression '$cmd <span style="color: #000099; font-weight: bold;">`$</span>args'<br />
###ALIAS###<br />
&quot;</span>@<br />
<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> <span style="color: #000066;">-Name</span> <span style="color: #660033; font-weight: bold;">$Alias</span> <span style="color: #000066;">-Value</span> <span style="color: #009900;">&quot;Alias$Alias&quot;</span> <span style="color: #000066;">-Description</span> <span style="color: #009900;">&quot;A UNIX-style alias using functions&quot;</span> <span style="color: #000066;">-Option</span> <span style="color: #009900;">&quot;AllScope&quot;</span> <span style="color: #000066;">-scope</span> Global <span style="color: #000066;">-passThru</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> unalias<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;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Alias</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Force</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Alias</span></span> <span style="color: #660033; font-weight: bold;">$Alias</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Description</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;A UNIX-style alias using functions&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Item</span></span> <span style="color: #009900;">&quot;function:Alias$Alias&quot;</span> <span style="color: #000066;">-Force</span>:<span style="color: #660033; font-weight: bold;">$Force</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Item</span></span> <span style="color: #009900;">&quot;alias:$alias&quot;</span> <span style="color: #000066;">-Force</span>:<span style="color: #660033; font-weight: bold;">$Force</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Removed alias '$Alias' and accompanying function&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Item</span></span> <span style="color: #009900;">&quot;alias:$alias&quot;</span> <span style="color: #000066;">-Force</span>:<span style="color: #660033; font-weight: bold;">$Force</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Removed alias '$Alias'&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>You can save that as alias.ps1 and create your aliases the way you used to in bash <code>alias ls=&#39;ls -recurse&#39;</code> and still be able to invoke them and, <strong>you can pass them extra parameters</strong> somewhat like in csh when you invoke them.  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':-)' class='wp-smiley' />   I&#8217;ve added some extra text in the function name and content and in the alias description, so it&#8217;s actually pretty easy to find all the aliases and functions this script creates and dump them to a file so you can reload them later if you want &#8230; but I haven&#8217;t actually written a function to do that yet myself. </p>

	<p>One important note: You <strong>must</strong> not use recursive aliases in the bindings on v1 &#8212; that is, <code>alias ls=&#39;ls -recurse&#39;</code>  will loop until it hits PowerShell&#8217;s recursive limit (only 100) and exit if you try to use it on v1 &#8212; because the script won&#8217;t be able to resolve the alias &#8220;ls&#8221; to Get-ChildItem &#8230; you&#8217;re probably better off not relying on that feature anyway.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powershell-power-user-tips-bash-style-alias-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell Power User Tips: Current Directory</title>
		<link>http://joelbennett.net/powershell-power-user-tips-current-directory/</link>
		<comments>http://joelbennett.net/powershell-power-user-tips-current-directory/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 00:36:28 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[CurrentDirectory]]></category>
		<category><![CDATA[Path]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[PUT]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-power-user-tips-current-directory/</guid>
		<description><![CDATA[This is the second in 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. Fixing the &#8220;Current Directory&#8221; problem The [...]]]></description>
			<content:encoded><![CDATA[	<p>This is the second in 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.  </p>

	<h5>Fixing the &#8220;Current Directory&#8221; problem</h5>

	<p>The core of this tip is very simple: Windows tracks your application&#8217;s &#8220;current directory&#8221; ... and you can get and set this location using static methods of the <code>System.IO.Directory</code> class: <code>SetCurrentDirectory</code> and <code>GetCurrentDirectory</code>.</p>

	<p>The reason this is showing up as a Power User Tip is that PowerShell doesn&#8217;t set this environment setting when you navigate &#8212; it uses it&#8217;s internal &#8220;PSProvider&#8221; architecture, and doesn&#8217;t differentiate between whether you&#8217;re in a FileSystem location or a registry location, or even a third-party provider. So, it never actually changes the current directory, and any console command or .net method you call which uses the current directory will most likely be in the wrong place &#8212; like, for instance:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$sw</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">IO</span>.<span style="color: #003366;">StreamWriter</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;NeatFile.txt&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$sw</span>.<span style="color: #003366;">writeline</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;I could write a lot of neat stuff here!&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$sw</span>.<span style="color: #003366;">close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span></div>

	<p>The problem is that now you don&#8217;t know where &#8220;NeatFile.txt&#8221; is &#8212; the &#8220;current directory&#8221; depends on how you launched PowerShell &#8212; most frequently it&#8217;s your <code>$Home</code> directory (equivalent to <code>Env:HOMEDRIVE + Env:HOMEPATH</code> &#8212; usually something like C:\Documents and Settings\YourName), but it could be your SystemRoot (C:\Windows) or the current directory of the app that launched PowerShell (eg: C:\Windows\System32 when you run it via &#8220;runas&#8221;). You can figure it out by running: <code>[IO.Directory]::GetCurrentDirectory()</code>.</p>

	<p>But here&#8217;s something more interesting: you can &#8220;fix&#8221; the problem by using <code>[IO.Directory]::SetCurrentDirectory</code>. There are a couple of catches, however: You can&#8217;t just use $pwd or <code>Get-Location</code> because you might be in the registry or some other location that&#8217;s not a Directory.  And you can&#8217;t just use <code>Get-Location -PSProvider FileSystem</code> because even though it returns the current <em>FileSystem</em> provider path, the FileSystem provider supports &#8220;fake&#8221; PSDrives (eg: you could create a Scripts: drive like <code>new-psdrive scripts filesystem &#34;$Home\Scripts&#34;</code>) and these aren&#8217;t actually supported by the .Net FileSystem.  Luckily, PowerShell includes a <code>Convert-Path</code> cmdlet which was created for this very purpose: converting a path from a Windows PowerShell path to a native path supported by the underlying provider.</p>

	<p>Without further ado, here&#8217;s a one-liner you can add to your prompt function:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">Directory</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SetCurrentDirectory</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Location</span></span> <span style="color: #000066;">-PSProvider</span> FileSystem<span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span></div>

	<p> <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: You can do the same thing using the <code>System.Environment</code> class, and it turns out that the ProviderPath is a property of the PathInfo object, perhaps you&#8217;ll find this syntax simpler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Environment<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">CurrentDirectory</span><span style="color: #66cc66;">=</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Location</span></span> <span style="color: #000066;">-PSProvider</span> FileSystem<span style="color: #333;">&#41;</span>.<span style="color: #003366;">ProviderPath</span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powershell-power-user-tips-current-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell Power User Tips: Get-Command precedence</title>
		<link>http://joelbennett.net/powershell-power-user-tips-get-command-precedence/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/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://joelbennett.net/powershell-power-user-tips-get-command-precedence/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

