<?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; PowerTips</title>
	<atom:link href="http://joelbennett.net/tag/powertips/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Tue, 31 Aug 2010 04:13:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerBoots: The tutorial walkthrough</title>
		<link>http://joelbennett.net/powerboots-tutorial-walkthrough/</link>
		<comments>http://joelbennett.net/powerboots-tutorial-walkthrough/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:21:13 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=963</guid>
		<description><![CDATA[Updated to PowerBoots 0.1 An introduction to PowerBoots Please excuse me if I start by just copying the basic ideas of the Shoes Tutorial, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start. PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions [...]]]></description>
			<content:encoded><![CDATA[	<h3> <img src='http://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' alt='[new]' class='wp-smiley' />  Updated to PowerBoots 0.1</h3>

	<h2>An introduction to PowerBoots</h2>

	<p>Please excuse me if I start by just copying the basic ideas of the <a href="http://shoooes.net/tutorial/">Shoes Tutorial</a>, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start.  PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions for writing Windows Presentation Framework (<span class="caps">WPF</span>) applications in the PowerShell scripting language.  You should get <a href="http://boots.CodePlex.com">the latest version of PowerBoots</a> before continuing, and install it by putting the &#8220;PowerBoots&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2). </p>

	<p><del>Don&#8217;t forget to start PowerShell.exe with the <span class="caps">STA</span> parameter</del> (This is no longer required in PowerBoots 0.1).</p>

	<p>Did I hear someone ask <strong>what <em>is</em> WPF?</strong> It was introduced as part of .Net 3.0 (and vastly improved in .Net 3.5), so you can expect to find it preinstalled on computers from Vista on, and of course you can download and install it on XP if it&#8217;s not already installed.  The only thing you really need to know about <span class="caps">WPF</span> for the purposes of this tutorial is that it is <strong>the</strong> new <span class="caps">GUI</span> toolkit for .Net, and that it is container based &#8212; you put elements into other elements to control the layout, rather like <span class="caps">HTML</span> and Java Swing&#8230; you can <strong>pick up the rest as we go along</strong>.</p>

	<h2>A simple Boots program</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots1.png" class="float-right-block" alt="" width="144" height="86" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-SizeToContent</span> WidthAndHeight <span style="color: #000066;">-Content</span> <span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span> <br />
<span style="color: #333;">&#41;</span><br />
<br />
&nbsp;</div>

	<p>The parenthesis ( and ) are a container, so the button is &#8220;in&#8221; the Window.  You can also pass a ScriptBlock instead, which works basically the same way. Of course, this is a bit uglier than the Shoes syntax, so lets see if we can&#8217;t clean it up some. The <code>-Content</code> parameter is positional, so the first non-named argument you pass will be used for that. The same is true for the -Children parameter of panels, and in fact, each of the other similar parameters: Items, Blocks, and Inlines.</p>

	<p>We have used a function <code>New-BootsWindow</code> which has an alias <code>Boots</code>. Boots takes all the same parameters as the <code>Window</code> function mentioned previously, but it uses slightly more useful defaults, and has a few other major benefits as well, the first of which is that it automatically &#8220;shows&#8221; the window, and the second is that it supports an <code>-Async</code> parameter which allows the window to come out in a new thread so that you can continue using PowerShell while the window remains alive and responsive.  There is one catch: New-BootsWindow <em>cannot</em> take it&#8217;s content on the pipeline (the <em>old function</em>, now renamed &#8220;Out-BootsWindow&#8221; can take pipeline content, but is a script function, and requires -<span class="caps">STA</span> mode) &#8212; you have to specify it as a ScriptBlock. So now that we know this, we can rewrite our first example like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Just for the record, the simplest Boots program would just be a simple popup dialog to put some text in a Window, like:  <code>Boots { $msg }</code> &#8230;</p>

	<h2>We can put controls in a stack</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots2.png" class="float-right-block" alt="" width="156" height="128" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>StackPanels are awesome. So are WrapPanels.  Try that code with a WrapPanel instead of a StackPanel and see what the difference is.  This brings up another point: those positional parameters we mentioned earlier: Content, Children, Items, Blocks, and Inlines, are also set to accept the value from the pipeline.  Not only that, but they are intelligent about whether or not the content model accepts multiple items! So we can actually rewrite that script like this, and get the same results:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> Button <span style="color: #66cc66;">|</span> StackPanel <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Now we&#8217;re really onto something! <span id="more-963"></span> For most of the rest of these examples, I&#8217;m going to stick with the former syntax, because with the indenting and parenthesis, it&#8217;s much easier for you to follow, especially if you&#8217;re not already familiar with <span class="caps">WPF</span>.  For instance, check out what this does:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #66cc66;">|</span> Button <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h2>Ok, lets see some formatting</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots3.png" class="float-right-block" alt="" width="174" height="152" /></p>

	<p>To scoot the buttons out from the edge we can use margins or padding: margins go on the outside of containers, padding goes on the inside.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Or, on one line:</span><br />
<br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (white) margin around it.  If you wanted the whole background of the window to be pink, you would need to set the background of the Window instead of the StackPanel.</p>

	<h2>Time for some artwork</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots4.png" class="float-right-block" alt="" width="176" height="164" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">60</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">80</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;20,10,60,20&quot;</span> <span style="color: #000066;">-Fill</span> Black <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>In Boots, everything always starts out white, and you must position things based on the container. You can see that the Margin can be specified as a single value as in the previous example, or as separate values for Left, Top, Right, Bottom.  Oddly, to satisfy PowerShell&#8217;s type-casting, you have to quote them so they&#8217;re a single comma-separated string, instead of four separate values.</p>

	<h2>Some more advanced drawing</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots5.png" class="float-right-block" alt="" width="148" height="158" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
Canvas <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Children</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;Rectangle <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,10,0,0&quot;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Stroke</span> Purple <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> Red<br />
&nbsp; &nbsp;Polygon <span style="color: #000066;">-Stroke</span> Pink <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> DarkRed <span style="color: #000066;">-Points</span> <span style="color: #009900;">&quot;10,60&quot;</span>, <span style="color: #009900;">&quot;50,60&quot;</span>, <span style="color: #009900;">&quot;50,50&quot;</span>, <span style="color: #009900;">&quot;65,65&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;50,80&quot;</span>, <span style="color: #009900;">&quot;50,70&quot;</span>, <span style="color: #009900;">&quot;10,70&quot;</span>, <span style="color: #009900;">&quot;10,60&quot;</span> <br />
<span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We use a Canvas for this because it can contain multiple items which are all absolutely positioned.  Unlike other containers, it doesn&#8217;t automatically expand to contain it&#8217;s children, so you typically have to set it&#8217;s size.</p>

	<p>We also have to set the Stroke and Fill.  These are the two colors that make up every object, if we don&#8217;t set them, they default to white. The StrokeThickness controls the line thickness.  Notice that we positioned the Rectangle by using the <code>Margin</code>, and positioned the arrow, which we built using a Polygon, based purely on the x,y coordinates of the points.  The available shapes are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.  You can, of course, make any shape you want to with the Polygon.</p>

	<p>There are other more advanced shapes available in external libraries, and we can even do 3D, use gradient or image fills&#8230;</p>

	<h3>We can even get images straight off the web</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>images<span style="color: #66cc66;">/</span>PowerBoots<span style="color: #66cc66;">/</span>IMG_3298.<span style="color: #003366;">jpg</span> <span style="color: #000066;">-MaxWidth</span> <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Now those are some powerful boots!&quot;</span> <span style="color: #000066;">-Async</span><br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots6.png" class="float-right-block" alt="" width="436" height="730" /></p>

	<p>Boots loads the image on a background thread, and caches it in memory, so the window will show up and be responsive while you&#8217;re waiting for the image, and because we&#8217;ve specified <code>-Async</code>, you can actually continue using PowerShell while the image loads. Note: it will load much faster the second time you run that script.  <img src='http://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/wink.gif' alt=';)' class='wp-smiley' /> </p>

<h3 style="clear: both;">Typography</h3>

	<p>PowerBoots doesn&#8217;t try to create a full set of typography-specific top-level elements the way Shoes does, because we are based on <span class="caps">WPF</span>, which has a far more powerful typography system available than any we&#8217;ve ever used.  So instead of having a bunch of named elements like banner, and title, and caption, and para and whatnot, we have controls based on how much text you want to put in them, and how much formatting you want to apply: Label is simplest, TextBlock supports limited text formattings, and FlowDocument supports full rich content. And of course, Hyperlink supports clicking  <img src='http://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/wink.gif' alt=';)' class='wp-smiley' /> </p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots7.png" class="float-right-block" alt="" width="466" height="202" /></p>

	<p>For the typography elements, the content model changes a bit.  There are basically two types: Inline and Block elements. The <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of a FlowDocument, it is actually a type-restricted &#8220;Items&#8221; container.  Instead of being able to have <em>anything</em> as content, it can only contain <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.aspx">Inline</a> flow content elements such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.anchoredblock.aspx">AnchoredBlock</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.bold.aspx">Bold</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx">InlineUIContainer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.italic.aspx">Italic</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.linebreak.aspx">LineBreak</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.span.aspx">Span</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx">Underline</a>, and it will create a run automatically if you just put a text string in it. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;A Question&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">42</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Foreground</span> <span style="color: #009900;">&quot;#FF0088&quot;</span> <br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Are you starting to dig &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #009900;">&quot;PowerBoots?&quot;</span> <span style="color: #000066;">-NavigateUri</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>tag<span style="color: #66cc66;">/</span>powerboots<span style="color: #66cc66;">/</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">NavigateUri</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">16</span> <span style="color: #000066;">-Inlines</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Inlines</span> <span style="color: #009900;">&quot;A. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Leave me alone, I'm hacking here!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Note: If you want support for the full <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">document model</a> (which allows Paragraphs and Lists), you need to use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentreader.aspx">FlowDocumentReader</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx">FlowDocumentPageViewer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a>, or a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentscrollviewer.aspx">FlowDocumentScrollViewer</a> ... there&#8217;s lots more information about those <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">on msdn</a>.</p>

	<h3>Events</h3>

	<p>If you were paying attention to that previous example, you&#8217;ll notice we just introduced event handling. Event handlers in PowerBoots are specified in much the same way that Properties are.  Their parameter names always start with &#8220;On_&#8221; and they take a script block.  The Hyperlink element in a <span class="caps">WPF</span> window doesn&#8217;t automatically open a browser (because you can use it to change &#8220;pages&#8221; in a <span class="caps">WPF</span> application), so to make simple web links work, you have to handle the &#8220;RequestNavigate&#8221; event as shown above.</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots8.png" class="float-right-block" alt="" width="202" height="76" /></p>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have a variable that points at the control(s) you want to affect.  You get a <code>$this</code> variable for free which points at the object that caused the event (eg: the Hyperlink in our previous example), but otherwise you need to handle this yourself. You can do that one of two ways: you can set a variable the way you normally would, and then use the variable in the form, or you can specify the variable name using the <code>-OutVariable</code> parameter.  Personally I prefer the latter, as it messes up the flow of code less, but it has the downside that the output variable is always an array, even when there&#8217;s only one item.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;WrapPanel &nbsp;$<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count<span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$label</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button ${global:Count} times!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$script</span>:label <span style="color: #66cc66;">=</span> Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$label</span> <span style="color: #666666; font-style: italic;"># You have to actually write-output the label</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Test App&quot;</span> <span style="color: #000066;">-On_Closing</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$global</span>:BootsOutput <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$global</span>:Count; <span style="color: #660033;">rm</span> variable:Count <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>I&#8217;ve made these examples slightly more complicated than they had to be to demonstrate some best practices.  When the Window is closed, the Out-Boots function returns the $BootsOutput variable &#8212; so if you want to <strong>output</strong> something from your gui, you need to set that variable.  You can, of course, access global scope variables using the scope prefix <code>$global:variableName</code>, so you can set many different variables which you read later in your script.  The catch is, sometimes the variables <em>have</em> to be explicitly set to script or even global scope in order to refer to the same variable in all of the event handlers&#8230;</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots9.png" class="float-right-block" alt="" width="260" height="84" /></p>

	<p>However, if you want to have this block of code actually <strong>output</strong> something into the pipeline, you&#8217;ll always want to use the <code>$BootsOutput</code> variable.  You can do that directly, the way the example above does, or you can simply use Write-Output! Inside the Out-Boots function, he Write-Output cmdlet just appends to the $BootsOutput variable &#8230; so it works pretty much exactly the way you would expect it to.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;WrapPanel <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#125;</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">++</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># You have to use array notation ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #660033;">Clear</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;You clicked the button $count times!&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-OutVariable</span> script:block <span style="color: #000066;">-VerticalAlignment</span> Center<br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>The first example outputs <strong>just</strong> the count of how many times you clicked.  The second outputs a series of numbers from 1 to however many times you click.  It&#8217;s your choice of how to work with it.</p>

	<h3>We can have fun with colors</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots10.png" class="float-right-block" alt="" width="318" height="286" /></p>

	<p>Boots gives you access to all the capabilities of the Windows Presentation Framework, but in some cases that comes at a cost, because we haven&#8217;t simplified their composability.  So we have RadialGradientBrush and LinearGradientBrush, but you have to specify the GradientStops etc &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #000066;">-Background</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;RadialGradientBrush $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F00&quot;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F90&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Label <span style="color: #009900;">&quot;Boots&quot;</span> <span style="color: #000066;">-HorizontalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-VerticalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Foreground</span> White <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">80</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-FontWeight</span> Bold &nbsp;<span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">40</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We also have</p>

	<h3>So what does an InputBox look like?</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots11.png" class="float-right-block" title="PowerBoots11.png" alt="PowerBoots11.png" width="260" height="86" /></p>

	<p>Well, the simplest possible input box is just a TextBox, with the Width set (if you don&#8217;t set the Width, a TextBox will adjust to fit it&#8217;s contents, which can be really distracting).  All you need to do is this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">220</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Enter your name&quot;</span> <span style="color: #000066;">-On_Close</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Text</span> <br />
<span style="color: #333;">&#125;</span> <br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots12.png" class="float-right-block" title="PowerBoots12.png" alt="PowerBoots12.png" width="200" height="76" /></p>

	<p>Of course, the problem with that is that it pops up this rather confusing window, when what we really wanted was a prompt, and an &#8220;Ok&#8221; button, and some event handling to make the thing behave the way we expect it to.  So lets try our first complicated form. </p>

	<p>I&#8217;ll warn you ahead of time of one thing I&#8217;m going to do here. I&#8217;m using the &#8220;Border&#8221; element to apply a colored border to the StackPanel (because it doesn&#8217;t have it&#8217;s own border parameters), but then I&#8217;m also using the WindowStyle and AllowsTransparency properties to remove the normal window chrome, creating the bare little popup you see in the screenshot. I handle the mouse down event on the main window to allow the user to drag the window around by clicking anywhere on it (except in the TextBox or on the Button, of course). Now it looks slick, and &#8212;you know&#8212; it works!</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;">BootsInput</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<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><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Prompt</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Please enter your name:&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Variable</span></span> textBox <span style="color: #000066;">-ErrorAction</span> SilentlyContinue<br />
&nbsp; &nbsp;Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Border <span style="color: #000066;">-BorderThickness</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-BorderBrush</span> <span style="color: #009900;">&quot;#BE8&quot;</span> <span style="color: #000066;">-Background</span> <span style="color: #009900;">&quot;#EFC&quot;</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> &nbsp;$<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$Prompt</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-OutVariable</span> global:textbox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">150</span> <span style="color: #000066;">-On_KeyDown</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &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;">$_</span>.<span style="color: #003366;">Key</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Return&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;Ok&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> `<br />
&nbsp; &nbsp;<span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$true</span> `<br />
&nbsp; &nbsp;<span style="color: #000066;">-On_PreviewMouseLeftButtonDown</span> <span style="color: #333;">&#123;</span> <br />
&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;">Source</span> <span style="color: #000066;">-notmatch</span> <span style="color: #009900;">&quot;.*\.(TextBox|Button)&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</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><br />
&nbsp;</div>

	<p>Hopefully you can follow that, although it&#8217;s obviously over the top  <img src='http://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/smile.gif' alt=':)' class='wp-smiley' /> . We handle the KeyDown event on the TextBox (if the Key is the Return key), and we also handle the click on the Button.  In both cases, we&#8217;ll write out the text that was entered, and use the special $BootsWindow variable to close the window.  We also handle the Load event for the window, to make sure the focus is on the TextBox, so you can just start typing.</p>

	<h3>A final example</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots13.png" class="float-right-block" title="PowerBoots13.png" alt="PowerBoots13.png" width="248" height="740" /></p>

	<p>I&#8217;ve got quite a few more examples I want to show off in part two of this tutorial, but to get you thinking about ways to integrate this with your routine tasks, and give you some ideas of what you can do with what you know already, let me give you this example of browsing photos, with a visual indication of how big the image file is. Of course, the point is that you could be visualizing, you know &#8230; anything.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">add-<span style="font-style: normal;">type</span></span> <span style="color: #000066;">-Assembly</span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span> &nbsp;<span style="color: #666666; font-style: italic;"># To get the Double-Click time</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</span></span> <span style="color: #333;">&#123;</span><br />
<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>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">$Label</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Name&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">$Value</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Length&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$width</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">200</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">double</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$margin</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickTime</span> <span style="color: #66cc66;">=</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;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">SystemInformation</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoubleClickTime</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<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><span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;IO&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">PSObject</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$InputObject</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$InputObject</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>.<span style="color: #660033; font-weight: bold;">$Value</span> <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This is the core part of the script ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## For each input, generate a grid panel with a label and a rectangle in the background</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GridPanel <span style="color: #000066;">-tag</span> @<span style="color: #333;">&#123;</span>item<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$io</span>; action<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-width</span> <span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #000066;">-margin</span> <span style="color: #660033; font-weight: bold;">$margin</span> $<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #660033; font-weight: bold;">$Label</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle <span style="color: #000066;">-HorizontalAlignment</span> Left <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#9F00&quot;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Width</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #009900;">&quot;$Value&quot;</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-On_MouseLeftButtonDown</span> <span style="color: #333;">&#123;</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;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666666; font-style: italic;"># They passed in a doubleclick action, so lets handle it</span><br />
&nbsp; &nbsp; &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;">$global</span>:ClickTime <span style="color: #000066;">-and</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$ClickTime</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalMilliseconds</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$global</span>:DoubleClickTime<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># We invoke the scriptblock </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and pass it the original input object </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and the grid panel object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$This</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Item</span> <span style="color: #660033; font-weight: bold;">$this</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:ClickTime <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</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;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> GraphLabel <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</span></span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example 1: list of processes with most RAM usage</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `kill`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ps</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">sort</span> PM <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; GraphLabel ProcessName PM <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Kill</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:panel<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Args</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; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-ov</span> global:panel<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">## Example 2: list of images, with file size indicated</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `open`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ls</span> ~<span style="color: #66cc66;">/</span>Pictures<span style="color: #66cc66;">/</span> <span style="color: #000066;">-recurse</span> <span style="color: #000066;">-Include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">jpg</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Sort</span> Length <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <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;">$Max</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Max</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">FullName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GraphLabel Name Length <span style="color: #000066;">-Max</span> <span style="color: #660033; font-weight: bold;">$Max</span> <span style="color: #000066;">-IO</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">FullName</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> WrapPanel <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">800</span><br />
&nbsp;</div>

	<p>Notice that I had to manually handle the concept of a double click, because StackPanels don&#8217;t have a Click or DoubleClick event, just MouseDown and MouseUp.  I could have stuck the stackpanel into something that does, but there&#8217;s really no need.  Also, the <code>[Diagnostics.Process]::Start</code> is the equivalent of typing the name into the run dialog.  I&#8217;m just <em>executing</em> the jpg, which makes it open in the default editor.  It&#8217;s just a sample, after all.</p>

	<h3>End note</h3>

	<p>The current version of Boots does not add threading support, which means that when you run something through Boots, execution of your script stops until the window is closed.  You can get around this somewhat in <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">various different ways</a>, but future releases <em>will</em> support running the <span class="caps">WPF</span> window in a separate thread, and even communicating to it &#8230; at the expense of a slightly different syntax. If you need that functionality, feel free to let me know &#8230; I could use some motivation.</p>

	<p>I hope you&#8217;ve enjoyed this tour through PowerBoots, and will be able to start applying it for fun and profit.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>PowerShell PowerUser Tips: List the Cmdlets in an Assembly</title>
		<link>http://joelbennett.net/powershell-poweruser-tips-list-the-cmdlets-in-an-assembly/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/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://joelbennett.net/powershell-power-user-tips-a-better-prompt/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/smile.gif' 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://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' 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://joelbennett.net/powershell-power-user-tips-a-better-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ideas for Writing Composable PowerShell scripts</title>
		<link>http://joelbennett.net/ideas-for-writing-composable-powershell-scripts/</link>
		<comments>http://joelbennett.net/ideas-for-writing-composable-powershell-scripts/#comments</comments>
		<pubDate>Wed, 28 May 2008 13:33:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/ideas-for-writing-composable-powershell-scripts/</guid>
		<description><![CDATA[I was just having some fun with some recent blog posts&#8230; WPF &#38; PowerShell &#8211; Part 5 has a script for &#8220;Get-Listbox&#8221; and for &#8220;Show-Control&#8221; and Halr9000 wrote a script he called Get-PSBlogroll I had modified the example from the WPF post to create a listbox which will &#8220;start&#8221; whatever you double click &#8230; Get-Listbox [...]]]></description>
			<content:encoded><![CDATA[	<p>I was just having some fun with some recent blog posts&#8230;</p>

	<p><a href="http://blogs.msdn.com/powershell/archive/2008/05/26/wpf-powershell-part-5-using-wpf-powershell-modules.aspx"><span class="caps">WPF</span> &amp; PowerShell &#8211; Part 5</a> has a script for &#8220;Get-Listbox&#8221; and for &#8220;Show-Control&#8221; and Halr9000 wrote a script he called <a href="http://halr9000.com/article/487">Get-PSBlogroll</a> </p>

	<p>I had modified the example from the <span class="caps">WPF</span> post to create a listbox which will &#8220;start&#8221; whatever you double click &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Listbox</span></span> Title <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">Control</span></span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;MouseDoubleClick&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">SelectedItem</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>And I tried something like this to let me launch links from hal9000&#8217;s blogroll:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PsBlogRoll</span></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;">HtmlUrl</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Listbox</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">Control</span></span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;MouseDoubleClick&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">SelectedItem</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Of course, what I&#8217;d like is to <em>see</em> the titles, instead of the <span class="caps">URL</span>, but because of how Get-ListBox was written, it only accepts strings, and therefore only returns strings.  I tried adding the <span class="caps">URL</span> as a NoteProperty on the title string, but there&#8217;s <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=345387&#38;amp;SiteID=99">a bug</a> in the script cmdlet implementation that strips <acronym title="Extended Type System">ETS</acronym> properties. So if you specify your parameter type as a [string], you loose the NoteProperty.</p>

	<h2>Idea 1: Use [PsObject] for your pipeline parameter</h2>

	<p>Of course, if they <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=345387&#38;amp;SiteID=99">fix the bug</a> this tip will go away, but in the meantime, you&#8217;re better off not casting the input from the PowerShell native <code>[PsObject]</code> type, because you loose any extended type system attributes (and if you cast <span class="caps">XML</span> to a string, it comes out yucky).</p>

	<h2>Idea 2: Don&#8217;t output xml nodes</h2>

	<p>Even after fixing the Get-Listbox script so it outputs what it gets in without casting it, I still have to craft PsObjects with the output from Halr9000&#8217;s script, because otherwise Get-ListBox just outputs the <code>.ToString()</code> representation of the xml node, which is invariably useless &#8212; I could data-bind to the xml attributes, but it requires a separate syntax in the <span class="caps">XAML</span> &#8212; which means you&#8217;d have to have separate code for xml vs objects.  So basically, you&#8217;re better off with a <code> | Select *</code> at the end of your Get-PsBlogRoll.</p>

	<h2>Idea 3: Display scripts should take a property (or a property list)...</h2>

	<p>What I eventually ended up doing is rewriting Get-Listbox to take a &#8220;Properties&#8221; parameter so that I could specify which of the attributes of an object I wanted to use &#8230; that way, I can pass in the almost unmodified output from Get-PSBlogRoll into the new Get-ListBox, and run with it.  In the end, it looks something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
cmdlet <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Listbox</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</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><span style="color: #660033; font-weight: bold;">$Input</span>,<br />
&nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</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;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Properties</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Begin</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$listItems</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Process</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&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;">$input</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">type</span><span style="color: #333;">&#93;</span></span> <span style="color: #000066;">-and</span> <span style="color: #660033; font-weight: bold;">$input</span>.<span style="color: #003366;">IsEnum</span> <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$listItems</span><span style="color: #66cc66;">+=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Enum<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetValues</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$input</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$input</span>.<span style="color: #003366;">GetType</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">IsEnum</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$listItems</span><span style="color: #66cc66;">+=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Enum<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetValues</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$input</span>.<span style="color: #003366;">GetType</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">else</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$listItems</span> <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$input</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">End</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$listbox</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;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Listbox</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$listBox</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$listItems</span> <span style="color: #666666; font-style: italic;"># | Select -unique</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># An optional DataTemplate, if they specified which properties they want to see</span><br />
&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;">$Properties</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;&lt;DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'&gt;&lt;StackPanel&gt;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$property</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Properties</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;">$xml</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;&lt;TextBlock Text='{Binding Path=$($property)}'/&gt;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">XML</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">+</span> <span style="color: #009900;">&quot;&lt;/StackPanel&gt;&lt;/DataTemplate&gt;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$listBox</span>.<span style="color: #003366;">ItemTemplate</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Windows.<span style="color: #003366;">Markup</span>.<span style="color: #003366;">XamlReader</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Load</span><span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366; font-weight: bold;">Xml</span>.<span style="color: #003366;">XmlNodeReader</span> <span style="color: #660033; font-weight: bold;">$xml</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$listBox</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: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PsBlogRoll</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$webClient</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;">Net</span>.<span style="color: #003366;">WebClient</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">xml</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Opml</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$webClient</span>.<span style="color: #003366;">DownloadString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;http://bloglines.com/export?id=halr9000&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$PsBlogroll</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Opml</span>.<span style="color: #003366;">opml</span>.<span style="color: #003366;">body</span>.<span style="color: #003366;">outline</span> <span style="color: #66cc66;">|</span> Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">text</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">'@Powershell'</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## The Select statement outputs a custom PSObject instead of the original (XmlNode) object</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$PsBlogroll</span>.<span style="color: #003366;">Outline</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Object</span></span> title, text, htmlUrl, xmlUrl<br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">PSBlogRoll</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Listbox</span></span> title <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">Control</span></span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;MouseDoubleClick&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># But now I can use the &quot;HtmlUrl&quot; property to launch it in my browser</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">SelectedItem</span>.<span style="color: #003366;">HtmlUrl</span> <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Of course, because of how I wrote the DataTemplate for the properties (using a StackPanel and a loop to output multiple TextBlock objects) you could choose to show the Url as well, and all you&#8217;d have to do is change the call to Get-Listbox like this: <code>Get-Listbox title,htmlUrl</code> &#8230; which is pretty cool.  Does anyone else have any ideas for making these even easier to compose?  </p>

	<p>A <em>really</em> nice trick would be if the wpf cmdlet could use <code>Write-Output</code> in the click handler and actually <code>yield</code> to allow the rest of the pipeline to process that item, but so far, I haven&#8217;t been able to find a way to do that (short of closing the <span class="caps">WPF</span> window, of course).</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/ideas-for-writing-composable-powershell-scripts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' 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/images/smilies/../../../wp-content/plugins/smilingmasses/smile.gif' 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/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' 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>
	</channel>
</rss>
