Writing and Publishing PowerShell Modules

Earlier in August we mentioned that modularity and abstraction are quite helpful. PowerShell modules can help enable these concepts.
You might ask "Modules... why can't I just write a function?" There are a number of benefits to bundling your functions into modules:

 

  • Simplify code organization
  • Group related functions together
  • Share state between functions, but not with the user
  • Re-use "helper functions" that you don't want exposed to the user
  • Improve discoverability:
    Find-Module MyModule
    Get-Command -Module MyModule
  • Simplify distribution:
    Install-Module MyModule
Where does that last bullet come from?

 

The PowerShell Gallery

If you've worked with Perl, you've probably used CPAN, which archives more than 150,000 modules. Other languages have similar tools, like PyPI for Python, or RubyGems for Ruby.

 

In the PowerShell world we've had a few community alternatives, but nothing official until late 2014, when Microsoft introduced the PowerShell Gallery. The gallery is still under limited preview, with less than 300 modules published.

 

The PowerShell community can benefit from the PowerShell Gallery through simplified and centralized discovery and distribution. We can find, install, or publish modules with a single command in PowerShell 5. Perhaps some day we will see a vibrant PowerShell community that extends beyond IT administration.

 

Write and Publish PowerShell Modules

Let's help build up the PowerShell Gallery. Do you write PowerShell modules at work or at home? Consider open sourcing them on GitHub, and publishing them in the PowerShell Gallery!

 

If you're comfortable writing PowerShell functions, but haven't started writing modules, check out Building a PowerShell Module, where we walk through the creation and publication of a PowerShell module.

 

Edit: This follow-up shows a simple way to automatically deploy your modules to the gallery.

 

Cheers!
About the Author

Warren Frame

Systems Engineer with a penchant for PowerShell, science, cooking, information security, family, cookies, and the Oxford comma.