{"id":72252,"date":"2015-07-10T00:01:00","date_gmt":"2015-07-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/10\/understanding-powershell-modules\/"},"modified":"2019-02-18T09:47:03","modified_gmt":"2019-02-18T16:47:03","slug":"understanding-powershell-modules","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/understanding-powershell-modules\/","title":{"rendered":"Understanding PowerShell Modules"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about Windows PowerShell modules.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! I keep hearing about Windows PowerShell modules, but I do not know what they really mean. Is it something that comes with Windows PowerShell, or is it something I have to buy or download? I don&rsquo;t get it. Can you shed some light?\n&mdash;ST\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello ST,\nMicrosoft Scripting Guy, Ed Wilson, is here. Things are really exciting around the Scripting Household. I have been talking to Microsoft evangelist ,Blain Barton, about doing a PowerShell Saturday in Florida. Without giving any details away, it is something we are exploring. It will be way cool!\nWindows PowerShell modules are a great way to share functions, and to be able to bring a group of functions into a script, or to a Windows PowerShell session, or even from computer to computer. The functions do not need to be advanced functions, but it sort of makes sense. I mean, if I am going to be writing something that I want to reuse, something I want to share from machine to machine, or something I want to with other users, it makes sense to use all of the Windows PowerShell technology that is available, including:<\/p>\n<ul>\n<li>Comment-based Help<\/li>\n<li>Parameter constraints<\/li>\n<li>Error handling<\/li>\n<li>Output streams (error, warning, and verbose)<\/li>\n<li>WhatIf (if the function will change system state)<\/li>\n<li>Confirm (if you want to ensure the user is aware of the potential action to take place, such as deleting a bunch of users)<\/li>\n<\/ul>\n<p>All of these are features that can easily be implemented in advanced functions. Modules simply provide an easy way to ship related functions. Modules can include the following:<\/p>\n<ul>\n<li>Functions<\/li>\n<li>Variables<\/li>\n<li>Aliases<\/li>\n<\/ul>\n<p>The cool thing is that when I import a module, I can select if I want to import one or more functions, or one or more variables or alias. This provides a lot of flexibility. In addition, I can add versions to my module, so I can specify if I want to import a specific version of the module.\nA module is simply a Windows PowerShell file with a .psm1 file extension. I can create it in the Windows PowerShell ISE, and when I save it, I specify that I want to save the .psm1 extension. After I have my module, I should create a module manifest for it.\nA module manifest is a simple text file with a .psd1 file extension. I can create it manually, but the best way to create it is by using the <b>New-ModuleManifest<\/b> cmdlet. I must specify the path to the module, but there are also lots of other parameters that I can specify. Here is the syntax for <b>New-ModuleManifest<\/b>:<\/p>\n<p style=\"margin-left:30px\">New-ModuleManifest [-Path] &lt;String&gt; [-AliasesToExport &lt;String[]&gt;] [-Author<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;String&gt;] [-ClrVersion &lt;Version&gt;] [-CmdletsToExport &lt;String[]&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-CompanyName &lt;String&gt;] [-Copyright &lt;String&gt;] [-DefaultCommandPrefix<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;String&gt;] [-Description &lt;String&gt;] [-DotNetFrameworkVersion &lt;Version&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-FileList &lt;String[]&gt;] [-FormatsToProcess &lt;String[]&gt;] [-FunctionsToExport<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;String[]&gt;] [-Guid &lt;Guid&gt;] [-HelpInfoUri &lt;String&gt;] [-ModuleList<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;Object[]&gt;] [-ModuleVersion &lt;Version&gt;] [-NestedModules &lt;Object[]&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-PassThru] [-PowerShellHostName &lt;String&gt;] [-PowerShellHostVersion<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;Version&gt;] [-PowerShellVersion &lt;Version&gt;] [-PrivateData &lt;Object&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-ProcessorArchitecture &lt;ProcessorArchitecture&gt;] [-RequiredAssemblies<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; &lt;String[]&gt;] [-RequiredModules &lt;Object[]&gt;] [-RootModule &lt;String&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-ScriptsToProcess &lt;String[]&gt;] [-TypesToProcess &lt;String[]&gt;]<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [-VariablesToExport &lt;String[]&gt;] [-Confirm] [-WhatIf] [&lt;CommonParameters&gt;]\nWhen I am creating a module manifest for a new module, I can specify the aliases I want the module to export, the functions to export, and even the variables I want it to export. I can use the asterisk wildcard character ( * ), or I can enumerate exactly which items I want the module to export.\nThis is useful when a module may contain what I call &#8220;helper functions&#8221; that are used only by other functions within my module. If a function does not make sense to export, I can effectively hide it via my manifest.\nWhen I use the <b>Get-Module<\/b> cmdlet, it returns information from the module manifest (which resides in the same folder as the module itself). This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Module ConversionModule<\/p>\n<p style=\"margin-left:30px\">ModuleType Version&nbsp;&nbsp;&nbsp; Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExportedCommands<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;- &nbsp; &nbsp; &nbsp; &nbsp; &#8212;&#8212;- &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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Script&nbsp;&nbsp;&nbsp;&nbsp; 6.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConversionModule &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {ConvertTo-celsius, ConvertT&#8230;\nAs shown here, I can pipe the output of the command to the <b>Format-List<\/b> cmdlet and see more information:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Module ConversionModule | fl<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ConversionModule<\/p>\n<p style=\"margin-left:30px\">Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : C:\\Users\\ed\\Documents\\WindowsPowerShell\\Modules\\ConversionModule\\Conv<span style=\"font-size:12px\">ersionModule.psm1<\/span><\/p>\n<p style=\"margin-left:30px\">Description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : a module that performs various unit conversions<\/p>\n<p style=\"margin-left:30px\">ModuleType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Script<\/p>\n<p style=\"margin-left:30px\">Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 6.0<\/p>\n<p style=\"margin-left:30px\">NestedModules&nbsp;&nbsp;&nbsp;&nbsp; : {}<\/p>\n<p style=\"margin-left:30px\">ExportedFunctions : {ConvertTo-celsius, ConvertTo-Fahrenheit, ConvertTo-Feet,<span style=\"font-size:12px\">&nbsp;ConvertTo-Kilometers&#8230;}<\/span><\/p>\n<p style=\"margin-left:30px\">ExportedCmdlets&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">ExportedVariables :<\/p>\n<p style=\"margin-left:30px\">ExportedAliases&nbsp;&nbsp; : {CTCS, CTFH, CTFT, CTKM&#8230;}\nST, that is all there is to using Windows PowerShell modules. To Script or Not to Script Week will continue tomorrow when I will talk about more cool stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about Windows PowerShell modules. &nbsp;Hey, Scripting Guy! I keep hearing about Windows PowerShell modules, but I do not know what they really mean. Is it something that comes with Windows PowerShell, or is it something I have to buy or download? I don&rsquo;t get it. Can you shed [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,52,3,4,45],"class_list":["post-72252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about Windows PowerShell modules. &nbsp;Hey, Scripting Guy! I keep hearing about Windows PowerShell modules, but I do not know what they really mean. Is it something that comes with Windows PowerShell, or is it something I have to buy or download? I don&rsquo;t get it. Can you shed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=72252"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=72252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}