Back to Blog Posts

Writing a Module For PowerShell 💻

April 16, 2021

What is a PowerShell Module?

A module in PowerShell is simply a package which contains some PowerShell code such as a function, member or cmdlet. These are more commonly written in PowerShell themselves however some utilize compiled DLLs or even a mixture of both.

The benefit to creating these modules is primarily to share them across an organization or making them publicly available via repositories such as the PowerShell Gallery.

Microsoft has a pretty good write-up on creating PowerShell modules but their example is some boring Show-Calendar function so let's do something a bit more interesting.

So what are we doing?

Let's build something fun. One site that has admittedly become a part of my daily routine is Inspirobot. In its own words, Inspirobot is an artificial intelligence dedicated to generating unlimited amounts of unique inspirational quotes for endless enrichment of pointless human existence.

Some of my favorite 'inspirational quotes' that have come from Inspirobot.

revolt pug steve

So let's make a PowerShell module that will generate some inspiration!

Getting Started

Let's start by making a PowerShell Module (.psm1) file. This allows other users to import your code via Import-Module.

Now let's add some functionality to it. After digging into the request made in our browser that https://inspirobot.me/ uses to generate our quote, we can see it makes a GET request to

https://inspirobot.me/api?generate=true

So we'll just write a really simple function to do that.

function Get-Inspiration {
    return (Invoke-WebRequest -Method "GET" -Uri "https://inspirobot.me/api?generate=true").Content
}

Next we're going to export the function so users can actually import them using Export-ModuleMember.

Export-ModuleMember -Function Get-Inspiration

Write your help comments!

You should always add comments for PowerShell's built in help system. The entire point of creating a module is to share it, and if you are doing so publicly, documentation is very important.

<#
 .Synopsis
  Generates a link to an inspirational quote.

 .Description
  Makes a web request to https://inspirobot.me/ and returns a link to
  the generated inspirationsal quote.

 .Parameter X
  We are not using inputs but if you were, you would describe them here.

 .Example
   # Generate an inspirational quote.
   Get-Inspiration
#>

psd1 File

psd1 stands for PowerShell Data. These files are essentially manifests which describe what is inside a PowerShell Module. For our case, since we only have a single .psm1 file, this is optional. However if your module is more complex or you intend on updating it frequently, using a manifest is recommended.

Luckily, PowerShell has a built in cmdlet for generating these New-ModuleManifest.

Conclusion

And that's pretty much it! Now you can import and use your newly created module!

Also, I've uploaded the source for this mini-project here https://github.com/kacesensitive/Get-Inspiration.