Publish Script Module PowerShell Gallery

Publish Script Module PowerShell Gallery

Hello,

I recently publish my StarWars script module on the PowerShell gallery. I thought I could share here the process it’s been. Honestly, I will use this as a reminder for the next module I will publish on the PowerShell gallery.

Publish Script Module PowerShell Gallery – Prerequisites

You need to have:

  1. A PowerShell Script Module
  2. A PowerShell Module Manifest
  3. Your NuGet API Key

Script Module

First, you need to create a module, a PSM1 file, put some code in it. At the end of the script module, you should export explicitly all the function, aliases, and variables that you need to export. To do this, you can use the Export-ModuleMember cmdlet:

Export-ModuleMember -Function New-StarWarsADUser,New-StarWarsADGroup,Add-StarWarsADUserToAdGroup,New-StarWarsADSite,Invoke-StarWarsTheme,Remove-StarWarsADUser,Remove-StarWarsADSite,Remove-StarWarsADGroup

Module Manifest

Then, you need to create a module manifest, to do this, we’ll use the New-ModuleManifest cmdlet.

New-ModuleManifest -Path "D:\Modules\StarWars\StarWars.psd1" `
    -RootModule StarWars.psm1 `
    -Author 'Emmanuel Demillière' `
    -CompanyName 'ItForDummies.net' `
    -ModuleVersion ([Version]::new(0.1)) `
    -RequiredModules ActiveDirectory `
    -Description 'This module will help you to create some users, groups and sites in your Active Directory based on Star Wars data. This module heavely use the swapi.co API.' `
    -PowerShellVersion 3.0 `
    -FunctionsToExport New-StarWarsADUser,New-StarWarsADGroup,Add-StarWarsADUserToAdGroup,New-StarWarsADSite,Invoke-StarWarsTheme,Remove-StarWarsADUser,Remove-StarWarsADSite,Remove-StarWarsADGroup `
    -Tags @('ActiveDirectory','ADDS','AD-DS','AD') `
    -ReleaseNotes 'Initial release.'

In this manifest, you want to put as many information as you can. Those information will be used in the Gallery:

Publish Script Module PowerShell Gallery - Web Page

Publish Script Module PowerShell Gallery – Web Page

If you have some module dependency that aren’t on the PowerShell gallery, you need to manually edit the PSD1 file to add those module in the PrivateData section, hereunder an example with the ActiveDirectory (from the Remote Server Administration Tools – RSAT) module:

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

    PSData = @{

        # Tags applied to this module. These help with module discovery in online galleries.
        Tags = 'ActiveDirectory','ADDS','AD-DS','AD'

        # A URL to the license for this module.
        # LicenseUri = ''

        # A URL to the main website for this project.
        # ProjectUri = ''

        # A URL to an icon representing this module.
        # IconUri = ''

        # ReleaseNotes of this module
        # ReleaseNotes = ''

        # ExternalModuleDependencies
        ExternalModuleDependencies = @('ActiveDirectory')
    
    } # End of PSData hashtable

} # End of PrivateData hashtable
Publish Script Module PowerShell Gallery - Module Manifest

Publish Script Module PowerShell Gallery – Module Manifest

If you don’t do this, the publish will fail:

Publish Script Module PowerShell Gallery - Active Directory Module Dependency Error

Publish Script Module PowerShell Gallery – Active Directory Module Dependency Error

NuGet API Key

You can find your own publish key on the PowerShell gallery once you’re signed in.

Publish Script Module PowerShell Gallery – Publish

Once you complete all the prerequisites, you can publish the module. You need to use the Publish-Module cmdlet:

Publish-Module -Name StarWars -NuGetApiKey $Key

Note: You can find a list of best practices for publishing a PowerShell module to the gallery here. I advise you to respect them.

Leave a Reply