<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Media And Microcode : Module</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/tags/Module/default.aspx</link><description>Tags: Module</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Microcode: All About Modules (Windows PowerShell CTP2)</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/2008/08/10/microcode-all-about-modules-windows-powershell-ctp2.aspx</link><pubDate>Sun, 10 Aug 2008 04:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8845501</guid><dc:creator>JamesBrundage</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/mediaandmicrocode/comments/8845501.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mediaandmicrocode/commentrss.aspx?PostID=8845501</wfw:commentRss><description>&lt;P&gt;&lt;A href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/27/microcode-debugging-cmdlets-and-providers-with-visual-studio-and-ctp2.aspx" mce_href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/27/microcode-debugging-cmdlets-and-providers-with-visual-studio-and-ctp2.aspx"&gt;In a previous post&lt;/A&gt;, I showed you how you can debug Windows PowerShell cmdlets and providers in Visual Studio and load them up without using installutil.exe.&amp;nbsp; This very briefly touched on using the command Add-Module, in this case, to load up an assembly containing cmdlets.&lt;/P&gt;
&lt;P&gt;This post explains how to use modules, a new feature in Windows PowerShell CTP2.&amp;nbsp; &lt;EM&gt;You must be using at least &lt;/EM&gt;&lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=7C8051C2-9BFC-4C81-859D-0864979FA403&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=7C8051C2-9BFC-4C81-859D-0864979FA403&amp;amp;displaylang=en"&gt;&lt;EM&gt;Windows PowerShell CTP2&lt;/EM&gt;&lt;/A&gt;&lt;EM&gt; or later to use this technology.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;From the previous post, you might assume that modules can only be DLLs.&amp;nbsp; This is not the case.&amp;nbsp; In fact, modules can either be a single script, a collection of scripts, or a DLL containing cmdlets or providers.&amp;nbsp; Modules allow you a cleaner way to work with groups of scripts, because they can contain private variables that only those scripts share.&amp;nbsp; You can also refer to a function within a module, which allows you to group commands more logically and to use more common function names without fear of overwriting the names of other functions.&lt;/P&gt;
&lt;P&gt;A previous post already talked about how you can use a DLL as a module ( by simply typing Add-Module &lt;EM&gt;DllPath&lt;/EM&gt; ), so I won't talk about that type of module much more today, except to point out the best advantage I can see:&amp;nbsp; Now I only write a cmdlet or provider and I do not have to write a snapin.&amp;nbsp; Perhaps because my fingers are busy enough as it is, I personally believe that shorter code is almost always better, so I think this is a pretty nice step to skip.&lt;/P&gt;
&lt;P&gt;Instead of looking at DLLs, I'll look at making modules out of scripts.&amp;nbsp; I have already introduced a few functions that are good candidates for the two types of modules that I want to use.&lt;/P&gt;
&lt;P&gt;The functions are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/13/microcode-windows-powershell-windows-desktop-search-problem-solving.aspx" mce_href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/13/microcode-windows-powershell-windows-desktop-search-problem-solving.aspx"&gt;Search-WindowsDesktop&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-get-recordedtv-with-select-object.aspx" mce_href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-get-recordedtv-with-select-object.aspx"&gt;Get-RecordedTV&lt;/A&gt;&amp;nbsp; (which depends on Search-WindowsDesktop)&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-dvr-with-get-recordedtv.aspx" mce_href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-dvr-with-get-recordedtv.aspx"&gt;Remove-DVRBySeries&lt;/A&gt; (which depends on Get-RecordedTV)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;These functions are a good case for modules because:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Search-WindowsDesktop is generally useful, but is not logically related to Get-RecordedTV.&lt;/LI&gt;
&lt;LI&gt;Remove-DVRBySeries and Get-RecordedTV, however, are logically related.&amp;nbsp; This pair also presents a case where I might only want to surface one of a pair of functions, or both (which is actually very easy, but we'll show that later.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Since Search-WindowsDesktop is the only search related function I've got at the moment, I'm going to put it in a module.&amp;nbsp; Ready?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Open Graphical PowerShell (run gpowershell.exe or All Programs&amp;nbsp; -&amp;gt; Windows PowerShell (CTP2) -&amp;gt; Graphical Windows PowerShell )&lt;/LI&gt;
&lt;LI&gt;Create a new script with CTRL + N (or File-&amp;gt;New Script )&lt;/LI&gt;
&lt;LI&gt;Copy/Paste Search-WindowsDesktop into Graphical PowerShell&lt;/LI&gt;
&lt;LI&gt;Save Search-WindowsDesktop as&amp;nbsp;Search-WindowsDesktop.ps1 in a subdirectory named search under $env:UserProfile\Documents\WindowsPowerShell\packages (CTP2) or $env:UserProfile\Documents\WindowsPowerShell\Modules (post CTP2)&lt;/LI&gt;
&lt;LI&gt;Create a file, search.psm1 in the search directory&lt;/LI&gt;
&lt;LI&gt;In search .psm1, add this line: .$psScriptRoot\Search-WindowsDesktop.ps1&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;That's it.&lt;/P&gt;
&lt;P&gt;Add an additional line for each other script you'd like to dot source into a module.&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;James Brundage [MSFT]&lt;!--BeginCode&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8845501" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Microcode/default.aspx">Microcode</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Scripting+Tricks/default.aspx">Scripting Tricks</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Search/default.aspx">Search</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Module/default.aspx">Module</category></item></channel></rss>