Steve Borba

My notes, I hope they help you, feel free to comment/add to them

First Powershell Module

I created my first powershell module, so I am going to put it in my own words so I can create another later if I wanted to.
First start making functions and store them by alone in .ps1 files. Create a folder structure with the supporting functions (ones users won’t user) in private and the ones they’ll use in public:

Then create the psd1 file with “New-ModuleManifest”

Then create the psm1 file with this:

#Get public and private function definition files.
    $Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
    $Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )

#Dot source the files
    Foreach($import in @($Public + $Private))
    {
        Try
        {
            . $import.fullname
        }
        Catch
        {
            Write-Error -Message "Failed to import function $($import.fullname): $_"
        }
    }

Export-ModuleMember -Function $Public.Basename

Now you should be able to locally use you module:

Import-Module "C:\Program Files\WindowsPowerShell\Modules\PAN-Power\PAN-Power.psd1"

Test things out, make sure they run, then sign up for PSGallery and get a key to upload, then finally publish it

Publish-Module -Name 'PAN-Power' -NuGetApiKey $Key

And just edit the psd1 with the new version number and publish again to update.

Install-Module -Scope CurrentUser -Name PAN-Power

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>