PowerShell

How to create a PowerShell Module with multiple Functions

In this blog post, I will show you how to create a module with multiple functions using an example. You will see that this is not rocket science. Let’s jump in.

In order to use multiple functions in a module, we have to declare them as functions to export. This also means we need a module manifest file with that statements in it.

Objective

The goal of this tutorial is to provide two functions in a module named Patrick. The two functions are called Get-SmallFiles and Get-BigFiles.

Creating a Module Folder “Patrick”

First of all, we need to create a module folder. We can choose between three folder paths. I will pick the first option, the folder is then created in C:\Program Files\Windows PowerShell, which means that this module will be available to all users.

Here is the code for my folder “Patrick”.

New-Item -Path ($env:PSModulePath -split ';')[0] -ItemType Directory -Name Patrick

Now we have a folder in the module folder!

Creating the Module Manifest File “patrick.psd1”

As mentioned before, we need a psd1 file which serves as our manifest file. We provide all the information needed, especially the two functions: Get-SmallFiles and Get-BigFiles.

$paramHash = @{
    Path = 'C:\Program Files\WindowsPowerShell\Modules\Patrick\patrick.psd1'
    RootModule = "patrick.psm1"
    Author = "Patrick Gruenauer"
    CompanyName = "sid-500.com"
    ModuleVersion = "1.0"
    Guid = '56f62aec-33ca-4d8a-8685-c6a87ac9a002'
    PowerShellVersion = "5.1"
    Description = "Testmodul"
    FormatsToProcess = ""
    FunctionsToExport = @('Get-BigFiles', 'Get-SmallFiles')
    AliasesToExport = ""
    VariablesToExport = ""
    CmdletsToExport = ""
   }
   
   New-ModuleManifest @paramHash

Let’s move on with the creation of the PowerShell Module file.

Creating the PowerShell Module File (psm1) with two functions: Get-SmallFiles and Get-BigFiles

Next, we configure the PowerShell module file with all the functions in it, in my case it is Get-SmallFiles and Get-BigFiles.

New-Item -Path 'C:\Program Files\WindowsPowerShell\Modules\Patrick' -ItemType File -Name patrick.psm1

The file is empty. Let’s put some content in it.

Add-Content -Path 'C:\Program Files\WindowsPowerShell\Modules\Patrick\patrick.psm1' `
-Value 'function Get-BigFiles {Get-ChildItem -Path $home -File -Recurse | Where-Object Length -gt (1MB)}; 
function Get-SmallFiles {Get-ChildItem -Path $home -File -Recurse | Where-Object Length -lt (1MB)}'

Mission accomplished.

How do you know this worked?

Open PowerShell 5 or PowerShell 7.

Type Get-Smallfiles and afterwards Get-BigFiles.

Hope this was useful. See you next time.

Categories: PowerShell

Tagged as: ,

5 replies »

  1. you’re writing to the all users module location, as such you’d have to have your powershell session elevated.
    That’s not ideal and could be considered risky, especially if you’re still developing the modules,.

    I’d recommend creating in your $PSHome module locations instead

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.