<?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; PowerShell Functions</title>
	<atom:link href="http://huddledmasses.org/tag/powershell-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>A guide to PowerShell&#8217;s Advanced Functions</title>
		<link>http://huddledmasses.org/a-guide-to-advanced-functions/</link>
		<comments>http://huddledmasses.org/a-guide-to-advanced-functions/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 03:44:03 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell 2]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1116</guid>
		<description><![CDATA[Someone asked on the PowerShell Newsgroup about writing Advanced Functions, and specifically: looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked on the <a href="news://msnews.microsoft.com:119/microsoft.public.windows.powershell">PowerShell Newsgroup</a> about writing Advanced Functions, and specifically:</p>

	<blockquote>
		<p>looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular cmdlets. What makes that possible? ... what do I need to do to make that happen? Whats the difference between an [advanced function] and a module?</p>
	</blockquote>

	<p>There are lots of articles on the Microsoft <a href="http://blogs.msdn.com/powershell/">PowerShell team blog</a> about both topics, but it seems there&#8217;s not really been any sort of step-by-step written, so I posted this to the newsgroup, and since the person who asked the original question said he found it useful, I figured I&#8217;d share it here&#8230;</p>

<span id="more-1116"></span>

	<h2>An Aside: Advanced Functions vs. Modules</h2>

	<p>Advanced functions and script modules are unrelated topics. A script module could have nothing but old PowerShell 1 -type functions, hypothetically, it could even not have any functions at all.  Advanced functions can be defined in a plain .ps1 script, in your profile, or by typing them in on the command-line, etc. The two things are essentially unrelated except for both being new features in PowerShell 2.0.</p>

	<h2>How To write an Advanced Function</h2>

	<h3>Step 1: Have something you want to do in a function.</h3>

	<p>I can&#8217;t emphasize enough how important it is that you should have a specific goal here. Writing a script for a specific purpose is much easier, because you don&#8217;t have to think about whether or not specific features are necessary, etc.</p>

	<p>For the purposes of this example, I want a function to start executables and return me the process object (so I can wait for it to finish, or whatever). I will call it Start-Process.</p>

	<h3>Step 2: Write a first step for the logic of your function. </h3>

	<p>Be sure to specify the parameters using the <code>function Name{Param(...)}</code> syntax rather than <code>function Name(...){}</code>, then you will be able to easily convert it to an advanced function by adding <code>[CmdletBinding()]</code> on the line right before the <code>Param</code> like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</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><span style="color: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</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;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</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;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</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: #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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 3: Start writing documentation and auto-help, including your parameters.</h3>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">################################################################</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><span style="color: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</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;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</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;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</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: #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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 4: Exploit advanced features by marking up your parameters.</h3>

	<p>In my case, I had a look at the signature of the start method by running <code>[Diagnostics.Process]::Start.OverloadDefinitions</code> and discovered that it can take credentials too.  So I wanted an <em>optional</em> credential parameter. I specified it without a <strong>position</strong>, and made it part of a non-default parameter set.</p>

	<p>I also wanted the <code>params</code> parameter to just take any additional parameters which I pass to the function, and pass them on to the process that I&#8217;m starting. </p>

	<p>Finally, I wanted the &#8220;App&#8221; parameter to be able to come from the pipeline. Specifically, I wanted to be able to pass the output of Get-Command or Get-ChildItem to it. In order to enable that, I defined an alias on it to the relevant properties of the objects which come from those two commands</p>

	<p>Hopefully you&#8217;re getting the idea of what&#8217;s possible here &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Credential</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;PSCredential containing valid login to &quot;run as&quot; another user.</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>DefaultParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;NoCreds&quot;</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; <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>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromPipelineByPropertyName<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;FullName&quot;</span>,<span style="color: #009900;">&quot;Path&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$app</span><br />
,<br />
&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>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;RunAs&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PSCredential&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&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;">PSCredential</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Credential</span><br />
,<br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromRemainingArguments<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#41;</span><br />
&nbsp; <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;">$params</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$credential</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cred</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">GetNetworkCredential</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</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;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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: #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;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</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: #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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&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; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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: #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;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</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: #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;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Bonus Step: Make the function be automatically defined</h3>

	<p>To be honest, there are a lot of ways to do this, here&#8217;s a few:</p>

	<h4>Option 1. Define it in your Profile.</h4>

	<p>Include the full text of the function in your Profile.ps1 script or Microsoft.PowerShell_profile.ps1 &#8230; just run <code>notepad $Profile</code> and paste the whole function in at the top, then save.</p>

	<h4>Option 2. Dot-source it in your Profile.</h4>

	<p>Save the function into a script file, e.g.: &#8220;StartProcessFunction.ps1&#8221; in your DocumentsWindowsPowerShell folder (next to your profile script) then put the line: <code>. StartProcessFunction.ps1</code> into your profile script to dot-source it. Using the dot causes the script to be loaded into the context as though you had put the contents of the script in, instead of the dot-source line. </p>

	<h4>Option 3. Import this function from a script file.  </h4>

	<p>This is basically the same as Option 2, except instead of &#8220;.&#8221; you can use <code>Import-Module</code>.  The Import-Module cmdlet, when used on a file with the .ps1 extension, is basically the same as dot-sourcing it, so there&#8217;s not a whole lot of benefit over dot-sourcing in this case.</p>

	<h4>Option 4. Put the function in a module.</h4>

	<p>You should <strong>only</strong> do this if you think <em>you might want to add additional functions</em> to the module. For instance, lets say you want to write a few other functions for working with process objects.  If you put them together in the same module (say: &#8220;ProcessUtility&#8221;) they can share script-scoped variables or private functions, and can be identified as being part of the same module using the <code>Get-Command -Module ProcessUtility</code> command. </p>

	<p>If you just have the single function, there&#8217;s really no point in changing it to a Module, but I&#8217;m not aware of any downside, either.</p>

	<h4>Option 4a. Make a module by just saving as a <code>.psm1</code></h4>

	<p>All you do is save the function into a module file, e.g.: &#8220;ProcessUtility.psm1&#8221; in your DocumentsWindowsPowerShell folder and then put the line: <code>Import-Module ProcessUtility.psm1</code> into your profile script.</p>

	<h4>Option 4b. Make a module folder.</h4>

	<p>This is by far the most extensible way to create modules. You paste the function into &#8220;ProcessUtility.psm1&#8221; and then save it into a folder &#8220;ProcessUtility&#8221; which must be created in one of your module folders. You can figure out where the module folders are by checking <code>$Env:PSMODULEPATH</code> &#8212; the normal spot is your DocumentsWindowsPowerShellModules. </p>

	<p>Once that&#8217;s done, you can import with the simpler command <code>Import-Module ProcessUtility</code> without any extension. </p>

	<p>The nice thing about creating the folder is that you can put any additional resources in that folder with it, and get at them using the <code>$PsScriptRoot</code> to define the folder. You can even add additional functions by putting them in separate files (which you can Import-Module from the main file, or you can define using a .psd1, but that&#8217;s <a href="http://huddledmasses.org/powershell-modules-metadata-and-mysteries/">another article</a>).</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/a-guide-to-advanced-functions/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

