{"id":4921,"date":"2009-01-02T12:46:00","date_gmt":"2009-01-02T12:46:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/02\/a-module-to-create-modules-and-advanced-functions\/"},"modified":"2019-02-18T13:13:00","modified_gmt":"2019-02-18T20:13:00","slug":"a-module-to-create-modules-and-advanced-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/a-module-to-create-modules-and-advanced-functions\/","title":{"rendered":"A Module to Create Modules and Advanced Functions"},"content":{"rendered":"<p>[1\/3\/09 Update &#8211; the original&nbsp;link to the attachment containing this code&nbsp;was broken but is now fixed.&nbsp; Sorry. jps]<\/p>\n<p>It is hard to overstate how important Modules and Advanced Functions are.&nbsp;&nbsp; If you are a PowerShell user &#8211; you need to take the time to learn these new mechanisms and use them as your first choice in implementing new functions.&nbsp; The reason for this is that these mechanisms are critical technologies to support SHARING.&nbsp; <\/p>\n<p>Advanced Functions provide help, parameter tab-completion, and cmdlet semantics that make it easy for other people to use your functions.&nbsp; Who wants to have to keep going back to read the source code to figure out what a function does &#8211; you want help.&nbsp; Who wants to guess at the parameters &#8211; you want tab completion.&nbsp; Who wants a functions that can have unintended consequences &#8211; you want -WHATIF -CONFIRM and -VERBOSE.<\/p>\n<p>Modules make it easy to package functions and use them in ways that they don&#8217;t collide with each other.&nbsp; BTW &#8211; I&#8217;m not sure whether we&#8217;ve been clear on this point or not but Modules provide an alternate mechanism to do what PSSNAPINs did.&nbsp; In fact MODULES work with both PSSCRIPT and DLLs.&nbsp; The benefit of Modules over snapins is that they can be xcopy deployed.<\/p>\n<p>The only downside of using Modules and Advanced Functions is that they both require extra work when you start writing code.&nbsp; That is why I wrote the Module Module (that is probably a horrible name but I&#8217;m going with it for now).&nbsp; This module makes it easy to create Modules and Advanced Functions AND it is a reasonable example of both.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS&gt; Import-Module Module <br \/>PS&gt; Get-Command -Module Module <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">CommandType&nbsp;&nbsp;&nbsp;&nbsp; Name&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; Definition <br \/>&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&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; &#8212;&#8212;&#8212;- <br \/>Function&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; New-GUID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230; <br \/>Function&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; New-ModuleTemplate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230; <br \/>Function&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; New-PSScript&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230; <\/font><\/p>\n<p>New-PSScript requires a VERB and NOUN and optionally takes a number of other parameters and generates the body of a script or a function including help comments.&nbsp; You can then redirect that output into a file.&nbsp; Here is an example:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS&gt; New-PSScript -Verb Get -Noun MyProcess -Parameter ID ` <br \/>&gt;&gt; -Synopsis &#8220;This is My Version of GetProcess&#8221; <br \/>&gt;&gt; <br \/>&lt;# <br \/>.Synopsis <br \/>&nbsp;&nbsp;&nbsp; This is My Version of GetProcess <br \/>.Description <br \/>&nbsp;&nbsp;&nbsp; QQDescription <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">.Parameter ID <br \/>.Example <br \/>&nbsp;&nbsp;&nbsp; Get-MyProcess <br \/>.ReturnValue <br \/>&nbsp;&nbsp;&nbsp; QQReturnValue <br \/>.Link <br \/>&nbsp;&nbsp;&nbsp; QQLink <br \/>.Notes <br \/>NAME:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Get-MyProcess <br \/>AUTHOR:&nbsp;&nbsp;&nbsp; RugratsVista\\jsnover <br \/>LASTEDIT:&nbsp; 01\/02\/2009 10:10:24 <br \/>#Requires -Version 2.0 <br \/>#&gt; <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">[CmdletBinding( <br \/>&nbsp;&nbsp;&nbsp; SupportsShouldProcess=$False, <br \/>&nbsp;&nbsp;&nbsp; SupportsTransactions=$False, <br \/>&nbsp;&nbsp;&nbsp; ConfirmImpact=&#8221;None&#8221;, <br \/>&nbsp;&nbsp;&nbsp; DefaultParameterSetName=&#8221;&#8221;)] <br \/>param( <br \/>[Parameter(Position=0)] <br \/>$ID <br \/>) <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">Begin <br \/>{ <br \/>}#Begin <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">Process <br \/>{ <br \/>&nbsp;&nbsp;&nbsp; Throw &#8220;Not Yet Implemented&#8221; <br \/>}#Process <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">End <br \/>{ <br \/>}#End<\/font><\/p>\n<p>New-ModuleTemplate does something similar for Modules.&nbsp; It requires a NAME and it generates a .PSD1 file describing the module and a .PSM1 file with a GET-NAME function.&nbsp; The nice thing about this command is that it takes a switch \u2013EDIT and if you specify it, it will open these files in Powershell_ISE so you can immediately start coding.&nbsp; <\/p>\n<p>Install the module by copying the files to a directory called MODULE in your module directory:<\/p>\n<p>($Env:PSMODULEPATH -Split &#8220;;&#8221;)[0]<\/p>\n<p>If you haven\u2019t already created your module directory, you can do this:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS&gt; New-Item \u2013Type Containter \u2013Force \u2013path ($Env:PSMODULEPATH -Split &#8220;;&#8221;)[0]<\/font><\/p>\n<p>Now if you are like me, you don\u2019t want to be typing that crap all the time so when you import-Module Module, the first thing it does is to create a PSDRIVE for you called MYMOD.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">New-PsDrive -Scope Global -Name MyMod -PSProvider FileSystem -Root (($env:PSMODULEPATH -split &#8220;;&#8221;)[0])<\/font><\/p>\n<p>Give these a try and let me know what you think.&nbsp; They all have HELP and the HELP has examples so party on.<\/p>\n<p>Again \u2013 it is hard to overstate how important it is to begin using Modules and Advanced Functions.&nbsp; In particular, you should start using them today and let us know if there are any bugs or problems or opportunities for improvement.&nbsp; We have a relatively short window to make any changes for V2 so start today.<\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT] <br \/>Windows Management Partner Architect <br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a> <br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/09\/26\/94\/96\/module.zip\">module.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[1\/3\/09 Update &#8211; the original&nbsp;link to the attachment containing this code&nbsp;was broken but is now fixed.&nbsp; Sorry. jps] It is hard to overstate how important Modules and Advanced Functions are.&nbsp;&nbsp; If you are a PowerShell user &#8211; you need to take the time to learn these new mechanisms and use them as your first choice [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,137,210,216],"class_list":["post-4921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-advanced-functions","tag-ctp3","tag-jeffrey-snover","tag-module"],"acf":[],"blog_post_summary":"<p>[1\/3\/09 Update &#8211; the original&nbsp;link to the attachment containing this code&nbsp;was broken but is now fixed.&nbsp; Sorry. jps] It is hard to overstate how important Modules and Advanced Functions are.&nbsp;&nbsp; If you are a PowerShell user &#8211; you need to take the time to learn these new mechanisms and use them as your first choice [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}