<?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; Tips</title>
	<atom:link href="http://huddledmasses.org/tag/tips/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>Parenthesis in PowerShell</title>
		<link>http://huddledmasses.org/parenthesis-in-powershell/</link>
		<comments>http://huddledmasses.org/parenthesis-in-powershell/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 17:04:02 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Q&A]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1613</guid>
		<description><![CDATA[I was asked recently to clarify the different uses of parenthesis in PowerShell: I struggle with $myvar.foo vs. ($myvar).foo vs. ($myvar).foo() vs. $myvar.foo(); vs. $myvar.foo(1), etc., etc. How do I know which one to use for which case? And then how to put this output into a write-output statement? There are basically three ways of [...]]]></description>
			<content:encoded><![CDATA[	<p>I was asked recently to clarify the different uses of parenthesis in PowerShell:</p>

	<blockquote>
		<p>I struggle with $myvar.foo vs. ($myvar).foo vs. ($myvar).foo() vs. $myvar.foo(); vs. $myvar.foo(1), etc., etc.  </p>
	</blockquote>

	<blockquote>
		<p>How do I know which one to use for which case? And then how to put this output into a write-output statement?</p>
	</blockquote>

	<p>There are basically three ways of using parenthesis in PowerShell: grouping, method calls, and sub-expressions.  Hopefully the explanations and examples below will help. Feel free to ask questions in the comments if I&#8217;ve missed something.</p>

	<h3>Grouping</h3>

	<p>The simplest use of parenthesis is similar to the mathematics use of grouping constructs. In fact, in it&#8217;s simplest form, it&#8217;s exactly the same as what you learned in math, and we use it to group calculations to determine the order of operations:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">4</span> <span style="color: #666666; font-style: italic;"># Output: 10</span><br />
<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">4</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Output: 10</span><br />
<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">4</span> <span style="color: #666666; font-style: italic;"># Output: 16</span><br />
&nbsp;</div>

	<p>But in PowerShell, it can also group calls to cmdlets and functions, and can allow you to invoke methods and properties on the output of those cmdlets.  This allows you to use operators or call methods on the output of cmdlets without first storing them in variables.  You can add and subtract dates:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">10</span><span style="color: #333;">&#41;</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;">date</span></span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">10</span><span style="color: #333;">&#41;</span></div>

	<p>When doing a registry lookup, you can avoid getting all of the PS* properties, you can call a single property, for instance, to get the path to PowerShell.exe:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ItemProperty</span></span> hklm:\SOFTWARE\Microsoft\<span style="color: #003366; font-weight: bold;">PowerShell</span>\<span style="color: #cc66cc;">1</span>\ShellIds\Microsoft.<span style="color: #003366; font-weight: bold;">PowerShell</span> Path<span style="color: #333;">&#41;</span>.<span style="color: #003366;">Path</span></div>

	<p>You can cast the output of a command to another type. So for instance, if you wanted to pick 10 random letters, you might use the fact that 65..90 are the <span class="caps">ASCII</span> values for A..Z, and then cast them to the char type:</p>

	<div class="posh code posh" style="font-family:monospace;"><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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span>65..90 <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Random</span></span> <span style="color: #000066;">-count</span> <span style="color: #cc66cc;">10</span><span style="color: #333;">&#41;</span></div>

	<p><div style="background:#efddb5"><div style="margin:20px"></p>

	<h4>Tips and Tricks</h4>

	<p>One side effect of this particular use of parentheses is that you can use them to cause an assigned value to leak: that is, you can assign the output of a command to a variable, and still output it. So for instance, this script actually returns 14 <strong>and</strong> 28:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$seven</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">4</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$seven</span><br />
<span style="color: #660033; font-weight: bold;">$seven</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3</span></div>

	<p>This is obviously a really useful shortcut sometimes, now that you know about it  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  ...<br />
</div></div></p>

	<h3>Method calls.  </h3>

	<p>All objects have methods. In order to invoke a method, you have to use parenthesis to &#8220;call: it. So for instance, strings have a bunch of different methods, which you can see with this command:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #009900;">&quot;hi&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Member</span></span> <span style="color: #000066;">-Type</span> Methods</div>

	<p>For instance to convert a string to all upper case, you call the ToUpper method:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #009900;">&quot;hi&quot;</span>.<span style="color: #003366;">ToUpper</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span></div>

	<p>Note that when you type the name of a method without the parenthesis, you don&#8217;t invoke it: <code>&#34;hi&#34;.ToUpper</code>  Instead, PowerShell does reflection on the method and gives you information about how to call it, what the overloads are, etc.  You can see much of the same information in the output of <code>Get-Member</code>.  When you have a method that takes parameters, you need to pass the parameters inside the parenthesis, separating them with commas if there&#8217;s more than one:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #009900;">&quot;This is a test&quot;</span>.<span style="color: #003366;">IndexOf</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;i&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;">#returns 2 (0-based index)</span><br />
<span style="color: #009900;">&quot;This is a test&quot;</span>.<span style="color: #003366;">IndexOf</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;i&quot;</span>, <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># start looking at 2, so returns 5, the NEXT i</span></div>

	<h3>Sub-Expressions</h3>

	<p>PowerShell also has another way to use parenthesis: with a leading $ (dollar sign), they become a sub-expression, which is calculated before the containing expression &#8230; so for instance, you will notice that the Write-Host inside the sub expression will output first:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span><span style="color: #333;">&#41;</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;">Date</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #000066;">-fore</span> yellow; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>timespan<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;2&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span></div>

	<p>Of course, you can&#8217;t put two command pipelines (separated by the semicolon &#8220;;&#8221;) into a simple parenthesis, but you can in a sub-expression! </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/parenthesis-in-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>I wanted to start my best-practices series…</title>
		<link>http://huddledmasses.org/what-i-did-today-instead/</link>
		<comments>http://huddledmasses.org/what-i-did-today-instead/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 20:41:06 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Quick]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1542</guid>
		<description><![CDATA[I was trying to write the first real post for that series today, but I got really distracted, and instead &#8230; Created a Virtual Launch Party for Lee Holmes&#8217; 2nd Edition Windows PowerShell Cookbook Updated my SharpSsh Module (which I wrote about ages ago in Scriptable SSH From PowerShell) to accept PSCredential objects or passwords, [...]]]></description>
			<content:encoded><![CDATA[	<p>I was trying to write the first real post for that series today, but I got <strong>really</strong> distracted, and instead &#8230;</p>

	<ul>
		<li>Created a <a href="http://j.mp/PSBlogCookBookParty">Virtual Launch Party</a> for Lee Holmes&#8217; 2nd Edition Windows PowerShell Cookbook</li>
		<li>Updated my <a href="http://poshcode.org/2099">SharpSsh Module</a> (which I wrote about ages ago in <a href="http://huddledmasses.org/scriptable-ssh-from-powershell/">Scriptable <span class="caps">SSH</span> From PowerShell</a>) to accept PSCredential objects or passwords, use CmdletBinding and have parameter set overrides</li>
		<li>Updated my <a href="http://poshcode.org/2098"><span class="caps">JSON</span> Module</a> to support Pipeline binding in ConvertFrom-Json and to call out the fact that it properly supports dynamic objects if you specify PSObject as the type.</li>
		<li>Updated (and finally released) my <a href="http://poshcode.org/2100">Presentation module</a></li>
	</ul>
	<ul>
		<li>Updated (and finally released) my <a href="/downloads/PowerTest.zip">PowerTest Module</a> (it now has setup/teardown and supports filtering tests by name and category). This module needs it&#8217;s own blog post too, but you can get an idea what it&#8217;s about in the first hour of the Live Meeting recording of my <a href="http://powershellgroup.org/rochester.ny/testing-with-powershell">Testing with PowerShell</a> presentation.</li>
	</ul>

	<p>Now that I mention all that stuff, there&#8217;s a few things from last week that bear mentioning too:</p>

	<ul>
		<li>I wrote a <a href="http://poshcode.org/2094">sweet little function</a> to wrap the <a href="http://j.mp/calcnet">LoreSoft.MathExpressions</a> assembly, which lets you do all sorts of math inline in the PowerShell console.</li>
		<li>I also <a href="http://poshcode.org/2097">updated my HttpRest module</a> to work with the latest 2.1 release of MindTouch Dream</li>
	</ul>
	<ul>
		<li>And published my current <a href="http://poshcode.org/2095">PowerShell Prompt</a> and <a href="http://poshcode.org/2096">PowerShell Profile</a></li>
	</ul>

	<p>And if you haven&#8217;t seen them &#8230; the week before that I also </p>

	<ul>
		<li>Published a function for <a href="http://poshcode.org/2060">invoking Generic Signature Methods from PowerShell</a> which was something I had previously considered impossible.</li>
		<li>Made some very nice updated to <a href="http://poshcode.org/2050">ConvertFrom-PropertyString</a> which parses ini files and anything like that&#8230;</li>
		<li>Published a trick for getting <a href="http://poshcode.org/2045">PowerShell.exe to run in .Net 4</a> and to allow PowerShell to <a href="http://poshcode.org/2045">load binary modules from network paths</a></li>
	</ul>
	<ul>
		<li>Wrote a function to <a href="http://poshcode.org/2043">generate proxy wrappers for OData Services</a> which was closely followed by a full <a href="http://psodata.codeplex.com/">PowerShell OData Module</a> from Doug Finke</li>
	</ul>

	<p>So, I&#8217;m giving myself a break.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/what-i-did-today-instead/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What Scope Am I In?</title>
		<link>http://huddledmasses.org/what-scope-am-i-in/</link>
		<comments>http://huddledmasses.org/what-scope-am-i-in/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 06:28:11 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerUser]]></category>
		<category><![CDATA[Scope]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Variable Scope]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1523</guid>
		<description><![CDATA[In PowerShell the question of scope is too complicated and convoluted. I&#8217;m going to try to help you understand it, but I&#8217;m not guaranteeing that I will be able to make it seem any simpler than it actually is. Hopefully, I won&#8217;t make it more complicated than it inherently is In PowerShell you always have [...]]]></description>
			<content:encoded><![CDATA[	<p>In PowerShell the question of scope is too complicated and convoluted. I&#8217;m going to try to help you understand it, but I&#8217;m not guaranteeing that I will be able to make it seem any simpler than it actually is. Hopefully, I won&#8217;t make it more complicated than it inherently is  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>In PowerShell you always have three named variable scopes: script, local and global. The default scope is always <strong>the same</strong> as the local scope, so when you set a variable without specifying the scope, it&#8217;s always set at local scope. One thing to note is that you can access these named scopes through the <code>$variable</code> notation, or through the variable drive, so all of the following are equivalent:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> <span style="color: #666699; font-weight: bold;">var</span> <span style="color: #009900;">&quot;one&quot;</span> <span style="color: #000066;">-Scope</span> Local<br />
<span style="color: #660033; font-weight: bold;">$var</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;one&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$local</span>:<span style="color: #666699; font-weight: bold;">var</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;one&quot;</span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Content</span></span> Variable::<span style="color: #003366;">Local</span>:<span style="color: #666699; font-weight: bold;">Var</span> <span style="color: #009900;">&quot;one&quot;</span><br />
&nbsp;</div>

	<p>A side note: the PSProvider drive notation means that when you&#8217;re in strict mode, if you want to test for the existence of a variable without causing an error, the simplest way to do it is with <code>Test-Path Variable:Var</code>&#8230;</p>

	<h3>What&#8217;s so hard about that?</h3>

	<p>Well, the question is: what scope are you in &#8220;right now&#8221; when a line of code is executing from a function or a script &#8230; </p>

	<p>Sometimes local scope is <span class="caps">ALSO</span> script scope, and sometimes, script scope is also global scope.  Specifically: when you&#8217;re typing at the console, all three scopes are the same. </p>

	<p>Let&#8217;s write a function to determine what named scope we&#8217;re in: </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Scope</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Variable</span></span> scope_test<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$GlobalScope</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;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> global <span style="color: #000066;">-ea</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ScriptScope</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;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> script <span style="color: #000066;">-ea</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$LocalScope</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>By setting a variable, and then testing the named scopes, we can verify what scope we were in when we set the variable. Of course, there&#8217;s no point testing the Local scope, because we already know that is where we are&#8230; then we return a hashtable of booleans indicating which scopes are active.</p>

	<p>If you dot-source that Get-Scope function, you&#8217;ll find the scope that you&#8217;re in where you dot-source it:</p>

	<ul>
		<li>if you do it at the interactive prompt it should tell you all three scopes are set</li>
		<li>if you do it in a script file it should tell you Local and Script</li>
		<li>but if you do it in a function, it will always just tell you &#8220;local&#8221; &#8212; and will not tell you if the function is in a script or not &#8230; nor how deep you are.</li>
	</ul>
	<ul>
		<li>and if you do it in a module, the results will depend on whether Find-Scope is defined in the module or not (this is very weird)</li>
	</ul>

	<h3>So are we done?</h3>

	<p>Not even remotely. On top of the named scopes, PowerShell also has nested scopes. Each script, function, scriptblock, etc. adds to the nested scopes, and takes you further from the global scope. On top of that, PowerShell has Modules. In a module, scope is flattened. Specifically, in a module, the default scope becomes the Script scope, which in this case is not actually reserved for a single file, but for any scripts, functions, etc that are executed from within that module&#8217;s context.</p>

	<p>To determine nesting level of the scope, we must do something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">trap</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">continue</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$depth</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">do</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">++</span><span style="color: #660033; font-weight: bold;">$depth</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$depth</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Test it like this:</span><br />
. <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span><br />
<span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span> . <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span> <span style="color: #333;">&#125;</span><br />
<span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span> . <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>As long as modules aren&#8217;t involved, that will tell you how many scopes there are between you and global scope (and thus, return zero when dot-source from the console commandline).</p>

	<p>This falls apart a bit if you&#8217;re in a module (in a module, you can&#8217;t get to global scope by increasing the scope value &#8212; effectively, the highest you can go is script scope, but in reality you can still access global scope by naming it). To determine if you&#8217;re in a module, you can simply check for the existence of the <code>$PSScriptRoot</code> variable, or verify that <code>$Invocation.MyCommand.Module</code> is set to something.</p>

	<p>Additionally, if you&#8217;re in a module, you need to define this function <strong>in that module</strong> for it to work at all.</p>

	<p>There&#8217;s one more thing you could use to help you learn what scope you&#8217;re in, and that is the Get-PSCallStack cmdlet. This will tell you how many commands have been called to get you where you are. It&#8217;s <em>usually</em> the same as the Scope Depth, but not when it&#8217;s in a script file, etc.</p>

	<p>Here&#8217;s my finished scope digging function:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Scope</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><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;">InvocationInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Invocation</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">trap</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">continue</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$depth</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">do</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">++</span><span style="color: #660033; font-weight: bold;">$depth</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$depth</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$depth</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ScopeDepth</span></span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Variable</span></span> scope_test<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Variable</span></span> scope_test<br />
<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> <span style="color: #003366; font-weight: bold;">PSObject</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; ModuleScope <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;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Invocation</span>.<span style="color: #003366;">MyCommand</span>.<span style="color: #003366;">Module</span><br />
&nbsp; &nbsp; &nbsp; GlobalScope <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;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> global <span style="color: #000066;">-ea</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ScriptScope <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;">bool</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Variable</span></span> scope_test <span style="color: #000066;">-Scope</span> script <span style="color: #000066;">-ea</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ScopeDepth &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$depth</span><br />
&nbsp; &nbsp; &nbsp; PipelinePosition <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Invocation</span>.<span style="color: #003366;">PipelinePosition</span><br />
&nbsp; &nbsp; &nbsp; PipelineLength <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Invocation</span>.<span style="color: #003366;">PipelineLength</span><br />
&nbsp; &nbsp; &nbsp; CallStack <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PSCallStack</span></span><br />
&nbsp; &nbsp; &nbsp; StackDepth <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PSCallStack</span></span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Count</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; Invocation <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Invocation</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<ul>
		<li>Remember: modules sometimes flatten scope.</li>
	</ul>
	<ul>
		<li>Remember: when calling this function you should dot-source it, and pass it $MyInvocation</li>
	</ul>

	<p>As as side note and counter-example, take for instance this module:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Module</span></span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">scope</span></span> <span style="color: #333;">&#123;</span> . <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Scope</span></span> <span style="color: #660033; font-weight: bold;">$MyInvocation</span> <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">nestedscope</span></span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">scope</span></span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">nestednestedscope</span></span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">nestedscope</span></span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span><br />
&nbsp;</div>

	<p>All three of those functions will return ScopeDepth = 2 (the test-scope function, and the module itself), but the StackDepth will increase correctly. I don&#8217;t know why. It&#8217;s late, so I&#8217;m going to stop writing before I get <em>more</em> confusing.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/what-scope-am-i-in/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Be a responsible geek &#8211; encrypt your email (free)</title>
		<link>http://huddledmasses.org/be-a-responsible-geek-encrypt-your-email-free/</link>
		<comments>http://huddledmasses.org/be-a-responsible-geek-encrypt-your-email-free/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 03:45:24 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Certificate]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Free]]></category>
		<category><![CDATA[Signing]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=695</guid>
		<description><![CDATA[Everyone should encrypt their emails &#8230; especially geeks, who can be expected to understand how little effort it would take for a rogue sysadmin or clever hacker to sniff your email on it&#8217;s way to it&#8217;s intended destination. Incidentally, if you&#8217;re a geek and you don&#8217;t know that, you should try running WireShark while you [...]]]></description>
			<content:encoded><![CDATA[	<p>Everyone should encrypt their emails &#8230; especially geeks, who can be expected to understand how little effort it would take for a rogue sysadmin or clever hacker to sniff your email on it&#8217;s way to it&#8217;s intended destination. </p>

	<p>Incidentally, if you&#8217;re a geek and you <strong>don&#8217;t</strong> know that, you should try running <a href="http://www.wireshark.org">WireShark</a> while you download and send emails&#8230;</p>

	<p>Anyway. The point of this email is to point out that Comodo (one of the biggest <span class="caps">SSL</span> Certificate vendors, and one who&#8217;s trust certificates are pre-installed on Windows) is <strong>giving away</strong> <a href="http://www.instantssl.com/ssl-certificate-products/free-email-certificate.html">free secure email certificates</a> which you can use in Outlook, Thunderbird, or whatever your favorite email client is. They&#8217;re actually on the <a href="http://office.microsoft.com/en-us/marketplace/EY010504841033.aspx">recommendation list</a> from Microsoft, but inexplicably listed under &#8220;other&#8221; office versions (they work fine in Outlook 2007, just to be clear).</p>

	<p>All you need to do now is run off and <a href="https://secure.instantssl.com/products/frontpage?area=SecureEmailCertificate">sign up</a> and then tell all your friends and family to do the same thing.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/be-a-responsible-geek-encrypt-your-email-free/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PowerShell PowerUser Tips: List the Cmdlets in an Assembly</title>
		<link>http://huddledmasses.org/powershell-poweruser-tips-list-the-cmdlets-in-an-assembly/</link>
		<comments>http://huddledmasses.org/powershell-poweruser-tips-list-the-cmdlets-in-an-assembly/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 05:07:54 +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[Reflection]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=663</guid>
		<description><![CDATA[Tiny script&#8230; let me know if you know a better way. function Get-Cmdlets &#123; param&#40;&#91;System.Reflection.Assembly&#93;$assembly&#41; $assembly.GetTypes&#40;&#41; &#124; Where-Object &#123; &#160; &#160; &#160;$_.GetCustomAttributes&#40;&#91;System.Management.Automation.CmdletAttribute&#93;,$false&#41; &#125; &#124; ForEach-Object &#123; &#160; &#160; &#160;$type = $_ &#160; &#160; &#160;$_.GetCustomAttributes&#40;&#91;System.Management.Automation.CmdletAttribute&#93;,$false&#41; &#160; &#125; &#124; Select VerbName, NounName, @&#123;n=&#34;Type&#34;;e=&#123;$type&#125;&#125; &#124; &#125; ## Example usage. ## You can use the [System.Reflection.Assembly]::Load... methods to get [...]]]></description>
			<content:encoded><![CDATA[	<p>Tiny script&#8230; let me know if you know a better way.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Cmdlets</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Reflection</span>.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$assembly</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$assembly</span>.<span style="color: #003366;">GetTypes</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">GetCustomAttributes</span><span style="color: #333;">&#40;</span><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;">CmdletAttribute</span><span style="color: #333;">&#93;</span></span>,<span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$type</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">GetCustomAttributes</span><span style="color: #333;">&#40;</span><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;">CmdletAttribute</span><span style="color: #333;">&#93;</span></span>,<span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span> &nbsp;<br />
<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> VerbName, NounName, @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Type&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$type</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example usage. </span><br />
<span style="color: #666666; font-style: italic;">## You can use the [System.Reflection.Assembly]::Load... methods to get an assembly</span><br />
<span style="color: #666666; font-style: italic;">## But for an example, use the &quot;CallingAssembly&quot; (System.Management.Automation )</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Cmdlets</span></span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Reflection</span>.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetCallingAssembly</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #660033;">Sort</span> VerbName, NounName <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Format-<span style="font-style: normal;">Table</span></span> <span style="color: #000066;">-auto</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-poweruser-tips-list-the-cmdlets-in-an-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell Power User Tips: A Better Prompt</title>
		<link>http://huddledmasses.org/powershell-power-user-tips-a-better-prompt/</link>
		<comments>http://huddledmasses.org/powershell-power-user-tips-a-better-prompt/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 03:49:30 +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[Scripting]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=628</guid>
		<description><![CDATA[For this edition of my Power User tips for PowerShell, I&#8217;m going to share my (heavily annotated) prompt function. Feel free to to copy useful pieces or just place the whole thing in your profile script I&#8217;m not going to say anything more, I&#8217;ll let the comments speak for themselves. Edit: Someone just pointed out [...]]]></description>
			<content:encoded><![CDATA[	<p>For this edition of my Power User tips for PowerShell, I&#8217;m going to share my (heavily annotated) prompt function. Feel free to to copy useful pieces or just place the whole thing in your profile script  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />   I&#8217;m not going to say anything more, I&#8217;ll let the comments speak for themselves.</p>

	<p><strong>Edit</strong>: Someone just pointed out that I forgot the bit of my prompt that sets my current path into the window title, and I realized I also forgot the bit that puts (Admin) in the title if you&#8217;re running &#8220;elevated&#8221; on Vista.</p>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: Ok, how many people noticed that I incorrectly used the Environement.CurrentDirectory when I set the WindowTitle (meaning it would only work right in FileSystem drives)?  Fixed now.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;"># Set-Prompt.ps1 (Dot-Source from your profile)</span><br />
<span style="color: #666666; font-style: italic;">###################################################</span><br />
<span style="color: #666666; font-style: italic;"># This should go OUTSIDE the prompt function, it doesn't need re-evaluation</span><br />
<span style="color: #666666; font-style: italic;"># We're going to calculate a prefix for the window title </span><br />
<span style="color: #666666; font-style: italic;"># Our basic title is &quot;PoSh - C:\Your\Path\Here&quot; showing the current path</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$global</span>:WindowTitlePrefix<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># But if you're running &quot;elevated&quot; on vista, we want to show that ...</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: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Environment</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">OSVersion</span>.<span style="color: #003366;">Version</span>.<span style="color: #003366;">Major</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">5</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span> <span style="color: #666666; font-style: italic;"># Vista and ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> Security.<span style="color: #003366;">Principal</span>.<span style="color: #003366;">WindowsPrincipal</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Security.<span style="color: #003366;">Principal</span>.<span style="color: #003366;">WindowsIdentity</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetCurrent</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># current user is admin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span>.<span style="color: #003366;">IsInRole</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Security.<span style="color: #003366;">Principal</span>.<span style="color: #003366;">WindowsBuiltInRole</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Administrator</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:WindowTitlePrefix <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PoSh (ADMIN)&quot;</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: #660033; font-weight: bold;">$global</span>:WindowTitlePrefix <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PoSh&quot;</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> <span style="color: #660033;">prompt</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># FIRST, make a note if there was an error in the previous command</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$err</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">!</span>$?<br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Make sure Windows and .Net know where we are (they can only handle the FileSystem)</span><br />
&nbsp; &nbsp;<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><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Also, put the path in the title ... (don't restrict this to the FileSystem</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">WindowTitle</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0} - {1} ({2})&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$global</span>:WindowTitlePrefix,<span style="color: #660033; font-weight: bold;">$pwd</span>.<span style="color: #003366;">Path</span>,<span style="color: #660033; font-weight: bold;">$pwd</span>.<span style="color: #003366;">Provider</span>.<span style="color: #003366;">Name</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Determine what nesting level we are at (if any)</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Nesting</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;$([char]0xB7)&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #660033; font-weight: bold;">$NestedPromptLevel</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Generate PUSHD(push-location) Stack level string</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Stack</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;+&quot;</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;">-Stack</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">count</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># my New-Script and Get-PerformanceHistory functions use history IDs</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># So, put the ID of the command in, so we can get/invoke-history easier</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># eg: &quot;r 4&quot; will re-run the command that has [4]: in the prompt</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$nextCommandId</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;">History</span></span> <span style="color: #000066;">-count</span> <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Id</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Output prompt string</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># If there's an error, set the prompt foreground to &quot;Red&quot;, otherwise, &quot;Yellow&quot;</span><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;">$err</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$fg</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$fg</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Yellow&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Notice: no angle brackets, makes it easy to paste my buffer to the web</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;[${Nesting}${nextCommandId}${Stack}]:&quot;</span> <span style="color: #000066;">-NoNewLine</span> <span style="color: #000066;">-Fore</span> <span style="color: #660033; font-weight: bold;">$fg</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #009900;">&quot; &quot;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-power-user-tips-a-better-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell Power User Tips: Bash-style &#8220;alias&#8221; command.</title>
		<link>http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/powershell-power-user-tips-current-directory/</link>
		<comments>http://huddledmasses.org/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://huddledmasses.org/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://huddledmasses.org/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://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>

