{"id":3364,"date":"2022-07-12T17:36:21","date_gmt":"2022-07-13T00:36:21","guid":{"rendered":"https:\/\/techthoughts.info\/?p=3364"},"modified":"2022-08-01T20:11:54","modified_gmt":"2022-08-02T03:11:54","slug":"powershell-modules","status":"publish","type":"post","link":"https:\/\/www.techthoughts.info\/powershell-modules\/","title":{"rendered":"PowerShell Modules"},"content":{"rendered":"\n<p>In this episode of the <a href=\"https:\/\/techthoughts.info\/learn-powershell-series\/\" target=\"_blank\" rel=\"noreferrer noopener\">Learn PowerShell<\/a> series we\u2019ll examine PowerShell Modules. In previous episodes we covered <a href=\"https:\/\/techthoughts.info\/powershell-scripts\/\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell scripts<\/a> and <a href=\"https:\/\/www.techthoughts.info\/powershell-functions\/\">PowerShell Functions<\/a> where we began to create solutions and solve problems using PowerShell. Many folks spend time at the scripts and functions stage as they experiment and get better with PowerShell. As you continue your PowerShell journey you may eventually find that others want to use what you create! To effectively share your code with others, you&#8217;ll want to look to creating PowerShell modules!<\/p>\n\n\n\n<p>A PowerShell module is simply a <em>psm1 file<\/em> that contains PowerShell logic. You can take a PowerShell script .<em>ps1<\/em> and save it as a <em>.psm1<\/em> file. This offers several advantages including:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Code can now be import using Import-Module<\/li><li>There is now control over what functions are import (public vs private functions)<\/li><li>Scope of variables controls<\/li><\/ul>\n\n\n\n<p>When combined with a .<em>psd1 file<\/em> you can take even further control of your module by providing additional metadata about the module project. A manifest also enables you to publish your module to the <a href=\"https:\/\/www.powershellgallery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell Gallery<\/a> for the entire world to use! This enables easy access your module with simple cmdlets like Install-Module.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<label for=\"ez-toc-cssicon-toggle-item-69e5373c18806\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e5373c18806\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Video\" title=\"Video\">Video<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#What_is_a_PowerShell_Module\" title=\"What is a PowerShell Module?\">What is a PowerShell Module?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#PowerShell_Module_Components\" title=\"PowerShell Module Components\">PowerShell Module Components<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#PowerShell_Module_File\" title=\"PowerShell Module File\">PowerShell Module File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#PowerShell_Module_Manifest\" title=\"PowerShell Module Manifest\">PowerShell Module Manifest<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Creating_a_PowerShell_Module_Manifest\" title=\"Creating a PowerShell Module Manifest\">Creating a PowerShell Module Manifest<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Installing_PowerShell_Modules\" title=\"Installing PowerShell Modules\">Installing PowerShell Modules<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Install_Module_from_Repository\" title=\"Install Module from Repository\">Install Module from Repository<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Install_Module_Manually\" title=\"Install Module Manually\">Install Module Manually<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Evaluating_a_PowerShell_Module\" title=\"Evaluating a PowerShell Module\">Evaluating a PowerShell Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Publishing_PowerShell_Modules_to_the_PowerShell_Gallery\" title=\"Publishing PowerShell Modules to the PowerShell Gallery\">Publishing PowerShell Modules to the PowerShell Gallery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#PowerShell_Module_Example\" title=\"PowerShell Module Example\">PowerShell Module Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Advanced_PowerShell_Module_Building\" title=\"Advanced PowerShell Module Building\">Advanced PowerShell Module Building<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/#Additional_Reading\" title=\"Additional Reading\">Additional Reading<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Video\"><\/span>Video<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you prefer video format over written documentation, I discuss this topic in the following TechThoughts video:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"video-container\"><iframe loading=\"lazy\" title=\"PowerShell Modules\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/tg5bEPsXTQY?feature=oembed&#038;wmode=opaque\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_PowerShell_Module\"><\/span>What is a PowerShell Module?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A <a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/understanding-a-windows-powershell-module?view=powershell-7\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell Module<\/a> is structured PowerShell code that you can reuse. You may want to reuse the code for your own projects, or make your solution available to others. You can share scripts, but they lack the capabilities that modules offer. For example, you can version control a PowerShell module. This gives users the ability to ensure they are using code with the latest features and bug fixes. Modules can also be hosted on <a href=\"https:\/\/powershellexplained.com\/2017-05-30-Powershell-your-first-PSScript-repository\/\" target=\"_blank\" rel=\"noreferrer noopener\">internal repositories<\/a> or on the <a href=\"https:\/\/www.powershellgallery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell Gallery<\/a> for easy access and installation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Module_Components\"><\/span>PowerShell Module Components<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Module_File\"><\/span>PowerShell Module File<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>A PowerShell Module consists of a few basic components with the primary being the <strong>PowerShell module file<\/strong>. This is represented with a <em>.psm1 <\/em>extension. The <em>psm1<\/em> is essentially a script that contains the functions and core logic of your module. This module file is the minimum requirement for a working PowerShell module.<\/p>\n\n\n\n<p>Lets say we have a script, <em><strong>learnpowershell.ps1<\/strong><\/em> which contains the function <strong>Get-LearnPowerShellInfo<\/strong>. You could simply save the <em>ps1<\/em> script as a <em>psm1<\/em>. This converts the script to a module, giving you the ability to import it as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# a psm1 module can now be imported\nImport-Module .\\learnpowershell.psm1\n\n# we can confirm that the module is loaded\nGet-Module learnpowershell\n\nModuleType Version    PreRelease Name                 ExportedCommands\n---------- -------    ---------- ----                 ----------------\nScript     0.0                   learnpowershell     Get-LearnPowerShellInfo\n\n# now that the module is imported we can see what commands are available\nGet-Command -Module learnpowershell\n\nCommandType     Name                                  Version    Source\n-----------     ----                                  -------    ------\nFunction        Get-LearnPowerShellInfo               0.0        learnpowershell\n<\/pre><\/div>\n\n\n<p>While this works, it lacks certain capabilities. For example, note that the module has a version of 0.0. This is because the module file itself does not have the ability to provide details such as the module version.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Module_Manifest\"><\/span>PowerShell Module Manifest<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>While it is technically possible to only use the PowerShell module file, most modules also include a module manifest. A module manifest is a file that contains a hash table of keys and values. These keys and values make up the <em>metadata<\/em> of your PowerShell module. This metadata provides a lot of detail about your module. It also enables you to take additional control over your module&#8217;s behavior. <\/p>\n\n\n\n<p>The manifest contains many elements. For a full list visit check out <a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/how-to-write-a-powershell-module-manifest?view=powershell-7\" target=\"_blank\" rel=\"noreferrer noopener\">How to write a PowerShell module manifest<\/a>. You can also view a <a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/how-to-write-a-powershell-module-manifest?view=powershell-7#sample-module-manifest\" target=\"_blank\" rel=\"noreferrer noopener\">sample module manifest<\/a>. Here are a few common elements that module authors typically leverage:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>RootModule &#8211; the <em>.psm1<\/em> that will be import by the manifest<\/li><li>ModuleVersion &#8211; version number of the module<\/li><li>GUID &#8211; unique identifier of the module for publication<\/li><li>Author &#8211; Author of the module<\/li><li>Description &#8211; functionality that the module provides<\/li><li>PowerShellVersion &#8211; minimum version of PowerShell that the module supports<\/li><li>RequiredModules &#8211; other modules that this module depends on<\/li><li>FunctionsToExport &#8211; your logic may contain many supporting functions in addition to public functions you want your users to use. You can surface just the public functions to your users here.<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_PowerShell_Module_Manifest\"><\/span>Creating a PowerShell Module Manifest<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>You can create a module manifest with a simple PowerShell command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# create a basic module manifest\nNew-ModuleManifest -Path C:\\learnpowershell\\learnpowershell.psd1\n<\/pre><\/div>\n\n\n<p>Open the manifest in your editor of choice and fill out the various elements. Once editing is complete, test the validity of the manifest edits by running:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# test a module manifest\nTest-ModuleManifest .\\learnpowershell.psd1\n\nModuleType Version    PreRelease Name                    ExportedCommands\n---------- -------    ---------- ----                    ---------------\nManifest   0.0.1                 learnpowershell\n<\/pre><\/div>\n\n\n<p>If you receive no errors the manifest passed validity check. You should now have two files:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>learnpowershell.psd1<\/strong> &#8211; the PowerShell Module Manifest containing meta data about the module<\/li><li><strong>learnpowershell.psm1<\/strong> &#8211; PowerShell Module file containing functions and logic<\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\nls\nDirectory: c:\\learnpowershell\n\nMode                 LastWriteTime         Length Name\n----                 -------------         ------ ----\n-a---            05\/30\/21    21:50           4156 learnpowershell.psd1\n-a---            05\/30\/21    18:38           9183 learnpowershell.psm1\n<\/pre><\/div>\n\n\n<p>With the psd1 in the mix you no longer import the psm1 directly. Import the manifest instead. Remember, the manifest specifies the root module that will be imported.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# import a module by specifying the manifest\nImport-Module .\\learnpowershell.psd1\n\n# we can confirm that the module is loaded\nGet-Module learnpowershell\n\nModuleType Version    PreRelease Name                   ExportedCommands\n---------- -------    ---------- ----                   ----------------\nManifest   0.0.1                 learnpowershell\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installing_PowerShell_Modules\"><\/span>Installing PowerShell Modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_Module_from_Repository\"><\/span>Install Module from Repository<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Using standard cmdlets like <strong><a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/PowershellGet\/Find-Module\" target=\"_blank\" rel=\"noreferrer noopener\">Find-Module<\/a><\/strong> and <strong><a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/PowershellGet\/Install-Module\" target=\"_blank\" rel=\"noreferrer noopener\">Install-Module<\/a><\/strong> you can install modules from a repository. The <a href=\"https:\/\/www.powershellgallery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell Gallery<\/a> is the most popular public repository and is registered by default.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# the PowerShell Gallery is registered by default\nGet-PSRepository\n\nName                      InstallationPolicy   SourceLocation\n----                      ------------------   --------------\nPSGallery                 Trusted              https:\/\/www.powershellgallery.com\/api\/v2\n\n# search for modules that are tagged with the Telegram key word\nFind-Module -Tag Telegram | Format-List\n\nName                       : PoshGram\nVersion                    : 1.16.0\nType                       : Module\nDescription                : PoshGram provides functionality to send various message types to a\n                             specified Telegram chat via the Telegram Bot API.\n\n# install a module from the PowerShell Gallery\nInstall-Module -Name PoshGram -Scope CurrentUser\n<\/pre><\/div>\n\n\n<p>It is possible to register additional repositories using the <strong><a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/PowershellGet\/Register-PSRepository\" target=\"_blank\" rel=\"noreferrer noopener\">Register-PSRepository<\/a><\/strong> cmdlet. Many organizations elect to <a href=\"https:\/\/powershellexplained.com\/2017-05-30-Powershell-your-first-PSScript-repository\/\" target=\"_blank\" rel=\"noreferrer noopener\">host their own<\/a> company-specific modules in an internal repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Install_Module_Manually\"><\/span>Install Module Manually<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also install PowerShell modules manually. The <em>psd1<\/em> and <em>psm1<\/em> simply need to be placed in a folder in the <strong>$env:PSModulePath<\/strong>. These directory paths differ depending on PowerShell version, operating system type, and scope. For instance, if you are installing a module for all users in PowerShell 7 on Windows it would be installed into the C:\\Program Files\\PowerShell\\Modules location. If you installed just for your user it would be placed in $env:USERPROFILE\\Documents\\PowerShell\\Modules. Reference a list of installation locations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PowerShell<ul><li>Windows<ul><li>$env:USERPROFILE\\Documents\\PowerShell\\Modules<\/li><li>C:\\Program Files\\PowerShell\\Modules<\/li><\/ul><\/li><li>Linux<ul><li>\/home\/user\/.local\/share\/powershell\/Modules<\/li><li>\/usr\/local\/share\/powershell\/Modules<\/li><\/ul><\/li><\/ul><\/li><li>Windows PowerShell<ul><li>C:\\Program Files\\WindowsPowerShell\\Modules<\/li><li>C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Once you&#8217;ve selected an installation destination, create a folder name that is the same name as the module itself. Then copy the <em>psd1<\/em> and <em>psm1<\/em> files into that directory. You can now import the module without specifying a direct path.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# change dir to the modules install folder\ncd $env:USERPROFILE\\Documents\\PowerShell\\Modules\n\n# create a new folder that is the same name as your module\nNew-Item -Name learnpowershell -ItemType Directory\n\n# copy the psd1 and psm1 files to this new directory\n\n# verify that PowerShell can now source that module\nGet-Module learnpowershell -ListAvailable\n\nDirectory: C:\\Users\\user\\Documents\\PowerShell\\Modules\n\nModuleType Version    PreRelease Name                                PSEdition ExportedCommands\n---------- -------    ---------- ----                                --------- ----------------\nManifest   0.0.1                 learnpowershell                     Desk\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Evaluating_a_PowerShell_Module\"><\/span>Evaluating a PowerShell Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can test and evaluate any module you find without directly installing it. Using the <strong><a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/PowershellGet\/Save-Module\" target=\"_blank\" rel=\"noreferrer noopener\">Save-Module<\/a><\/strong> cmdlet you can download a module from a repository to a directory of your choosing. From there you can import it manually for testing and evaluation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# save a module to a directory of your choice\nSave-Module PoshGram -Path C:\\eval -Repository PSGallery\n\n# change directory to the downloaded module\ncd C:\\eval\\PoshGram\\1.16.0\n\n# import the module directly for testing and evaluation\nImport-Module .\\PoshGram.psd1\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Publishing_PowerShell_Modules_to_the_PowerShell_Gallery\"><\/span>Publishing PowerShell Modules to the PowerShell Gallery<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As a first step, you&#8217;ll need to <a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/gallery\/how-to\/publishing-packages\/creating-an-account\" target=\"_blank\" rel=\"noreferrer noopener\">register an account<\/a> on the PowerShell Gallery website. This is a simple process, especially if you already have a Microsoft account. <\/p>\n\n\n\n<p>Armed with a login, you&#8217;ll be able to click on your username which will take you to the page that contains your API key.&nbsp; This API key is required to Publish scripts or modules to the PowerShell Gallery. Once you have your API key you&#8217;re ready to get your script or module published.<\/p>\n\n\n\n<p>Publishing modules is relatively straightforward. The module needs to be installed on the current machine you wish to publish from. This is because <strong><a href=\"https:\/\/docs.microsoft.com\/powershell\/module\/powershellget\/publish-module\" target=\"_blank\" rel=\"noreferrer noopener\">Publish-Module<\/a><\/strong> has to be able to locate the module. If you are creating a module locally, simply install it manually as shown above.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# publish your module to the PowerShell Gallery\nPublish-Module -Name \"learnpowershell\" -NuGetApiKey \"your-api-key-goes-here\" -Repository PSGallery\n<\/pre><\/div>\n\n\n<p>Don&#8217;t forget to also check out the <a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/gallery\/concepts\/publishing-guidelines\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/gallery\/concepts\/publishing-guidelines?view=powershell-7.1\">PowerShellGallery Publishing Guidelines and Best Practices<\/a>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Module_Example\"><\/span>PowerShell Module Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are thousands of modules on the Gallery. Most of them add real world functionality to your PowerShell install! For the purposes of this episode, I created a basic PowerShell module, <strong>LearnPowerShell<\/strong>. Here are a couple of tasks that you can complete to get hands on with an actual PowerShell module:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Search for the module (<strong>LearnPowerShell<\/strong>) on the PowerShell Gallery<\/li><li>Install the module on your device automatically or manually<\/li><li>Locate the module on your device and find the <em>psd1<\/em> and <em>psm1<\/em> files<\/li><li>Explore the <em>psm1<\/em> file and what it contains<\/li><li>Explore the <em>psd1<\/em> file. What metadata does it have about the module?<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advanced_PowerShell_Module_Building\"><\/span>Advanced PowerShell Module Building<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>At this point we&#8217;ve established that all that is needed for a working PowerShell module is the <em>.psm1<\/em>, likely paired with a <em>.psd1<\/em>. But, when looking at some open source PowerShell module projects like <a href=\"https:\/\/github.com\/techthoughts2\/PoshGram\" target=\"_blank\" rel=\"noreferrer noopener\">PoshGram<\/a>, (and many others) you will find a <em>great deal<\/em> many more files and folders in these projects.<\/p>\n\n\n\n<p>Many module authors integrate CI\/CD practices into their module development efforts. They leverage automated tests, check for proper formatting, generate help, and combine functions together to build the project for publication.<\/p>\n\n\n\n<p>Despite this added complexity you&#8217;ll find that each project still tracks towards creating a <em>.psm1<\/em> and <em>.psd1<\/em> as the final product. This is then published for others to use. We will cover advanced PowerShell module building techniques in a later lesson.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Additional_Reading\"><\/span>Additional Reading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/understanding-a-windows-powershell-module?view=powershell-7\" target=\"_blank\" rel=\"noreferrer noopener\">Understanding a Windows PowerShell Module<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/how-to-write-a-powershell-script-module?view=powershell-7\" target=\"_blank\" rel=\"noreferrer noopener\">How to Write a PowerShell Script Module<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/developer\/module\/how-to-write-a-powershell-module-manifest?view=powershell-7\" target=\"_blank\" rel=\"noreferrer noopener\">How to write a PowerShell module manifest<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/gallery\/how-to\/publishing-packages\/publishing-a-package?view=powershell-7.1\" target=\"_blank\" rel=\"noreferrer noopener\">Creating and publishing an item<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/powershell\/scripting\/gallery\/concepts\/publishing-guidelines?view=powershell-7.1\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShellGallery Publishing Guidelines and Best Practices<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this episode of the Learn PowerShell series we\u2019ll examine PowerShell Modules. In previous episodes we covered PowerShell scripts and PowerShell Functions where we began to create solutions and solve problems using PowerShell. Many&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3581,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[421],"tags":[265],"series":[436],"class_list":["post-3364","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-powershell","series-learn-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PowerShell Modules - Tech Thoughts<\/title>\n<meta name=\"description\" content=\"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.techthoughts.info\/powershell-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Modules - Tech Thoughts\" \/>\n<meta property=\"og:description\" content=\"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.techthoughts.info\/powershell-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"Tech Thoughts\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/techthoughts2\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-13T00:36:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-02T03:11:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jake\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:site\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/\"},\"author\":{\"name\":\"Jake\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\"},\"headline\":\"PowerShell Modules\",\"datePublished\":\"2022-07-13T00:36:21+00:00\",\"dateModified\":\"2022-08-02T03:11:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/\"},\"wordCount\":1447,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg\",\"keywords\":[\"PowerShell\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-modules\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/\",\"url\":\"https:\/\/www.techthoughts.info\/powershell-modules\/\",\"name\":\"PowerShell Modules - Tech Thoughts\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg\",\"datePublished\":\"2022-07-13T00:36:21+00:00\",\"dateModified\":\"2022-08-02T03:11:54+00:00\",\"description\":\"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-modules\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg\",\"width\":1280,\"height\":720,\"caption\":\"Learn PowerShell Episode 13 - PowerShell Modules\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-modules\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tech Thoughts\",\"item\":\"https:\/\/www.techthoughts.info\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.techthoughts.info\/#website\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"name\":\"Tech Thoughts\",\"description\":\"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog\",\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.techthoughts.info\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.techthoughts.info\/#organization\",\"name\":\"TechThoughts\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"width\":110,\"height\":109,\"caption\":\"TechThoughts\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/techthoughts2\",\"https:\/\/x.com\/JakeMorrison\",\"https:\/\/www.youtube.com\/TechthoughtsInfo2\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\",\"name\":\"Jake\",\"description\":\"A little about me...\",\"sameAs\":[\"https:\/\/jakewmorrison.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Modules - Tech Thoughts","description":"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.techthoughts.info\/powershell-modules\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Modules - Tech Thoughts","og_description":"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!","og_url":"https:\/\/www.techthoughts.info\/powershell-modules\/","og_site_name":"Tech Thoughts","article_publisher":"https:\/\/www.facebook.com\/techthoughts2","article_published_time":"2022-07-13T00:36:21+00:00","article_modified_time":"2022-08-02T03:11:54+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","type":"image\/jpeg"}],"author":"Jake","twitter_card":"summary_large_image","twitter_creator":"@JakeMorrison","twitter_site":"@JakeMorrison","twitter_misc":{"Written by":"Jake","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#article","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/"},"author":{"name":"Jake","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d"},"headline":"PowerShell Modules","datePublished":"2022-07-13T00:36:21+00:00","dateModified":"2022-08-02T03:11:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/"},"wordCount":1447,"commentCount":0,"publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","keywords":["PowerShell"],"articleSection":["Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.techthoughts.info\/powershell-modules\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.techthoughts.info\/powershell-modules\/","url":"https:\/\/www.techthoughts.info\/powershell-modules\/","name":"PowerShell Modules - Tech Thoughts","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","datePublished":"2022-07-13T00:36:21+00:00","dateModified":"2022-08-02T03:11:54+00:00","description":"Learn how to use and create PowerShell Modules. Dive into what makes up a PowerShell Module, and how to share your code using modules!","breadcrumb":{"@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.techthoughts.info\/powershell-modules\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#primaryimage","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","width":1280,"height":720,"caption":"Learn PowerShell Episode 13 - PowerShell Modules"},{"@type":"BreadcrumbList","@id":"https:\/\/www.techthoughts.info\/powershell-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tech Thoughts","item":"https:\/\/www.techthoughts.info\/"},{"@type":"ListItem","position":2,"name":"PowerShell Modules"}]},{"@type":"WebSite","@id":"https:\/\/www.techthoughts.info\/#website","url":"https:\/\/www.techthoughts.info\/","name":"Tech Thoughts","description":"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog","publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.techthoughts.info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.techthoughts.info\/#organization","name":"TechThoughts","url":"https:\/\/www.techthoughts.info\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","width":110,"height":109,"caption":"TechThoughts"},"image":{"@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/techthoughts2","https:\/\/x.com\/JakeMorrison","https:\/\/www.youtube.com\/TechthoughtsInfo2"]},{"@type":"Person","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d","name":"Jake","description":"A little about me...","sameAs":["https:\/\/jakewmorrison.com"]}]}},"jetpack_featured_media_url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2022\/08\/ep13_powershell_modules.jpg","jetpack_shortlink":"https:\/\/wp.me\/p2EFB0-Sg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3364"}],"collection":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/comments?post=3364"}],"version-history":[{"count":111,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3364\/revisions"}],"predecessor-version":[{"id":3577,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/3364\/revisions\/3577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media\/3581"}],"wp:attachment":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media?parent=3364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/categories?post=3364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/tags?post=3364"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/series?post=3364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}