<?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; Syntax</title>
	<atom:link href="http://huddledmasses.org/tag/syntax/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Parenthesis in PowerShell</title>
		<link>http://huddledmasses.org/parenthesis-in-powershell/</link>
		<comments>http://huddledmasses.org/parenthesis-in-powershell/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 17:04:02 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Q&A]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Tips]]></category>

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

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

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

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

	<h3>Grouping</h3>

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

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

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

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

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

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

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

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

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

	<h4>Tips and Tricks</h4>

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

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

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

	<h3>Method calls.  </h3>

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

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

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

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

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

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

	<h3>Sub-Expressions</h3>

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

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

	<p>Of course, you can&#8217;t put two command pipelines (separated by the semicolon &#8220;;&#8221;) into a simple parenthesis, but you can in a sub-expression! </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/parenthesis-in-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using Notepad++ For PowerShell Editing</title>
		<link>http://huddledmasses.org/using-notepad-plus-plus-for-powershell-editing/</link>
		<comments>http://huddledmasses.org/using-notepad-plus-plus-for-powershell-editing/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 02:09:08 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Notepad++]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scintilla]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Syntax Highlighting]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1337</guid>
		<description><![CDATA[A while back Thell Fowler (with a little help, and a lot of testing from me) wrote a very good PowerShell Lexer for Notepad++ 5.2 and later&#8230; it&#8217;s very thorough, has good code-folding, and full support for PowerShell 2.0 syntax highlighting. I mention this because Notepad++ 5.6 just released yesterday, and it has built-in support [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1341" class="wp-caption alignright" style="width: 310px"><a href="http://huddledmasses.org/wordpress/wp-content/uploads/2009/12/PowerShell_Lexer_Notepad++.png"><img src="http://huddledmasses.org/wordpress/wp-content/uploads/2009/12/PowerShell_Lexer_Notepad++-300x271.png" alt="The style I use has a nice black background..." title="PowerShell_Lexer_Notepad++" class="size-medium wp-image-1341" height="271" width="300" /></a><p class="wp-caption-text">The style I use has a nice black background...</p></div>

	<p>A while back Thell Fowler (with a little help, and a lot of testing from me) wrote a <strong>very</strong> good <a href="http://poshcode.org/notepad++lexer">PowerShell Lexer</a> for <a href="http://notepad-plus.sourceforge.net/uk/site.htm">Notepad++</a> 5.2 and later&#8230; it&#8217;s very thorough, has good <a class="zem_slink" href="http://en.wikipedia.org/wiki/Code_folding" title="Code folding" rel="wikipedia">code-folding</a>, and full support for PowerShell 2.0 syntax highlighting.</p>

	<p>I mention this because Notepad++ 5.6 just released yesterday, and it has built-in support for PowerShell syntax courtesy of <a class="zem_slink" href="http://www.scintilla.org/" title="Scintilla (editing component)" rel="homepage">Scintilla</a> ... but it&#8217;s <strong>very,</strong> <strong>very</strong> bad. The scintilla PowerShell lexer is probably the most minimal PowerShell lexer I&#8217;ve seen (it&#8217;s worse than the old &#8220;user style&#8221; I had created for Notepad++) and has no support for:</p>

	<p>	<ul>
		<li>The ` <a class="zem_slink" href="http://en.wikipedia.org/wiki/Escape_character" title="Escape character" rel="wikipedia">escape character</a> </li>
		<li>Here-strings (which can contain quotes, etc)</li>
		<li>The difference between strings and literal strings and literal here-strings</li>
		<li>The begin/process/end block keywords and Param() </li>
		<li>PowerShell operators (like -is or -gt or -notcontains)</li>
		<li>[System.Namespace.Class]::Method() syntaxes</li>
		<li>Nested $variables inside strings</li>
		<li>Nested $( code blocks ) inside strings (with strings inside those, and &#8230;)
		<li>Any of the new PowerShell 2 syntax like:
	<ul>
		<li>multi-line comments</li>
		<li>[Parameter()] and [Alias()] and [Validate &#8230;. ]</li>
	</ul></li>
	</ul>
	<ul>
		<li>[CmdletBinding()]</li>
	</ul></li></p>

	<p>There&#8217;s probably more, but I couldn&#8217;t be bothered to spend more than a couple of minutes with it. As you can probably guess &#8230; <strong>all of those features</strong> are supported by the external PowerShell Lexer plugin that Thell wrote, so if you&#8217;re a PowerShell and Notepad++ user, I apologize for not drawing your attention to our <a href="http://poshcode.org/notepad++lexer">PowerShell Lexer for Notepad++</a> before  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> .</p>

	<p>Incidentally, I stuck a screenshot in this post so you can see how <strong>I</strong> use it, but there&#8217;s one a more complete example of the <a href="http://poshcode.org/notepad++lexer">PowerShell Syntax Highlighting</a> on that lexer download page.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/fdf36a60-04ff-42dc-9722-b30ccfb8d28b/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=fdf36a60-04ff-42dc-9722-b30ccfb8d28b" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/using-notepad-plus-plus-for-powershell-editing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PowerBoots and Attached Properties</title>
		<link>http://huddledmasses.org/powerboots-and-attached-properties/</link>
		<comments>http://huddledmasses.org/powerboots-and-attached-properties/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 05:53:49 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Attached Properties]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1055</guid>
		<description><![CDATA[WPF uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just [...]]]></description>
			<content:encoded><![CDATA[	<p><span class="caps">WPF</span> uses a concept called &#8220;Attached Properties&#8221; to handle certain things, like when you put controls into a DockPanel. Basically, anything you put inside a DockPanel has a property &#8220;Dock&#8221; which you can set &#8230; but because the property is actually defined by the DockPanel, it doesn&#8217;t show up in PowerBoots, so you can&#8217;t just say <code>-Dock &#34;Bottom&#34;</code> when you create the child items &#8230; at least, not yet.</p>

	<p>I&#8217;m still thinking about the best way for me to expose this in PowerBoots, because the problem is that attached properties can get attached several ways, and not just on direct children of an element. So I wanted to show you a way that you can do it <em>for now</em> so you can at least use the DockPanel effectively:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Right</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Dock</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Bottom</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h2>A better way &#8230;</h2>

	<p>Here&#8217;s what I&#8217;m thinking about for the next release of PowerBoots (you can take this and use it now, if you like it, but I&#8217;m really looking for, uhm &#8230; better ideas).  Basically, I have written a function: <code>Set-AttachedProperty</code>, which takes an attached property and a value, and passes through the element on the pipeline. You may want to use this in conjunction with the module I published awhile back for creating type accelerators, because it lets you run a line like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Accelerator</span></span> DockPanel System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">DockPanel</span></div>

	<p>Which will let you substitute <code>[DockPanel]</code> for <code>[System.Windows.Controls.DockPanel]</code> &#8230; and of course, you can use it for all the types you want to use attached properties from, and it&#8217;s a real lifesaver if you need to use a bunch of them.  Of course, in this particular example, we really only need to use a single attached property, so it&#8217;s enough to define that property ahead of time:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$DockProperty</span> <span style="color: #66cc66;">=</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;">Controls</span>.<span style="color: #003366;">DockPanel</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DockProperty</span></div>

	<p>Then you can use the <code>Set-AttachedProperty</code> function through an alias <code>sap</code>, and rewrite that huge block above like this:</p>

	<div class="posh code posh" style="font-family:monospace;">Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; DockPanel <span style="color: #000066;">-LastChildFill</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Padding</span> <span style="color: #009900;">&quot;2,0,2,0&quot;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Right<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-HorizontalAlignment</span> Stretch<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> sap <span style="color: #660033; font-weight: bold;">$DockProperty</span> Bottom<br />
&nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-AcceptsReturn</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #000066;">-minWidth</span> <span style="color: #cc66cc;">300</span> <span style="color: #000066;">-minHeight</span> <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Pretty slick, right? And it will even print out the list of values in the error message if you invoke it with an invalid value against an enum property like dock:  <code>sap $DockProperty &#34;&#34;</code> &#8230; The problem I have with it is that you have to predefine your <code>$DockProperty</code> variable, and you can&#8217;t just define it against the root class.  So I&#8217;m trying to find a way to tweak the dynamic property generation to make it so that the pipe into <code>| sap $DockProperty Bottom</code>  can just be a parameter to the original element: <code>-Dock Bottom</code> &#8230; if I can&#8217;t find that, in the worst case scenario, I&#8217;ll just add an -AttachedProperties parameter with a hashtable of $DockProperty,&#8220;Bottom&#8221; or something &#8230; <em>what do you think?</em>  </p>

	<p>In any case, here&#8217;s the sap function, for now, and remember to use it as part of the pipeline:</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;">Set-<span style="font-style: normal;">AttachedProperty</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 />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</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;">$true</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">DependencyProperty</span><span style="color: #333;">&#93;</span></span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Property</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">DYNAMICPARAM</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</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;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameterDictionary</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</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;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">RuntimeDefinedParameter</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Name</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Value&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># $Param1.Attributes.Add( (New-ParameterAttribute -Position 1) )</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Attributes</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span> <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;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">ParameterAttribute</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> Position <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">ParameterType</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Property</span>.<span style="color: #003366;">PropertyType</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$paramDictionary</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Value&quot;</span>, <span style="color: #660033; font-weight: bold;">$Param1</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$paramDictionary</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">SetValue</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Property</span>, <span style="color: #660033; font-weight: bold;">$Param1</span>.<span style="color: #003366;">Value</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Element</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Alias</span></span> sap <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AttachedProperty</span></span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerboots-and-attached-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

