7
Powershell Modules and Nuget Repo
Gabe edited this page 2026-07-01 18:41:22 +00:00

Description

Project to take Powershell functions, turn them into Powershell modules which can be packaged and published to NuGet repository for easy distribution.

Usage:

Creating and Publishing a Powershell Module

  1. Under your Gitlab project, create a COMPANYNAME.FUNCTIONNAME folder
  2. Under your COMPANYNAME.FUNCTIONNAME folder, create a file called COMPANYNAME.FUNCTIONNAME.psm1
    • OR you can upload a psm1 file to your Gitlab Project
  3. Edit your COMPANYNAME.FUNCTIONNAME.psm1 and code your function(s) into the psm1 file and save file
    • Make sure it is in a folder named after the module.
  4. Pipeline does the creating of the manifest file, packaging, and publishing to the Nuget repo

Downloading Modules from the NuGet repo

  1. Add the Nuget Repository (PowerShell script). If you already have the repository you may skip this step(script checks and exits if the repo exists already) and proceed to step 2. Otherwise instructions in the expand box:
Click to expand
  • Run the Add Nuget Repository script:
  1. Open Powershell as Administrator > paste code block > run > profit
Invoke-Expression $(([convert]::ToString( (Invoke-WebRequest 'https:/URLHERE.ps1' -Cred (Get-Credential)) )) -replace "\?\?\?","" )
  • If you get an '-iwr is not recognized' error when running the above command on your older servers there is a command you can run to correct the TLS setting:
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
  1. Finding Modules:
    • To find a module: Find-Module -Repository 'NuGet' -Credential $Creds
      • Read 'Commands' section for examples on Find-Module command
  2. Installing Modules:
    • Install Module: Install-Module -Name <MODULENAME> -Repository NuGet -Credential $Creds -Scope CurrentUser
      • Read 'Commands' section for more info on the Install-Module command

Using Modules

  1. Once the module has been installed(or imported), you can simply use the function name (NOT the modules name) to run

Updating Modules:

  • Update-Module -Credential $Creds
    • Command updates all installed modules to the newest version
    • Can add "-Name MODUELNAME" to only update a certain module

Support

For help or questions, reach out to: Gabe Kerntke

Extra info:

Commands

Click to expand

Most commands will require:

$Creds = Get-Credential -Message "Enter user account and password"
NOTE ^ The credentials captured by the Get-Credential cmdlet are not stored on disk by default. Instead, they are returned as a PSCredential object and stored in memory within the current PowerShell session (e.g., in a variable). The password component of the PSCredential object is stored as a System.Security.SecureString in memory, which is an encrypted format that prevents it from being viewed as plain text in typical scenarios.
OR oneline, replace "-Credential $Creds" with -Cred (Get-Credential -Message "Enter user account and password")
  • Get-PSRepository
    • Shows all current powershell repositories on the system
  • Unregister-PSRepository -Name "Artifactory"
    • Command to remove a repository (replace NuGet with your repository name)
  • Find-Module
    • Shows all modules, or shows modules with a certain name, or shows all modules in a specific repo
    • Examples:
      • All modules in all repositories: Find-Module -Credential $Creds
      • All modules in a certain repository: Find-Module -Repository NuGet -Credential $Creds
      • Certain module in a certain repository: Find-Module -Repository 'NuGet' -Name 'Show-Calendar' -Credential $Creds
        • Can wildcard the name as well*
      • All versions of a certain module in a certain repository: Find-Module -Repository 'NuGet' -Name 'Show-Calendar' -AllVersions -Credential $Creds
  • Install-Module
    • Command will obtain the module from the repo and install it on your server, making it available for use.
    • Use the ''
      • Note: Modules are automatically placed in 1 of 2 spots when using the above command:
        • C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
        • C:\Users\env:USERNAME\Documents\WindowsPowerShell\Modules
    • 'RequiredVersion' if want to specify which version to install
  • Import-Module
    • Command will bring the module and its functions into your current powershell session, if the module is installed.
  • Uninstall-Module
    • Command will uninstall said module from your device
    • Examples:
      • Uninstall-Module -Name "Show-Calendar"
      • Uninstall-Module -Name "Show-Calendar" -AllVersions
  • Get-InstalledModule
    • Command lists all modules installed from a repository
    • Examples:
      • To get install modules only from NuGet repo: Get-InstalledModule | Where-Object { $_.Repository -eq 'Nuget' }
      • To get all installed versions of a certain module: Get-InstalledModule -Name "Show-Calendar" -AllVersions

Note: If the module is installed in a location included in your $env:PSModulePath environment variable, you can simply import it by its name instead of installing.

Require Modules

Click to expand

When you have a script that requires a module, you can add a requires statement to the top of your script. This will require that the specified module is loaded before your script runs. If the module is installed and if auto-loading is allowed, the requires statement will go ahead and import the module.
#Requires -Modules GetInfo

Manual Methods:

Manual method for creating a manifest file

Click to expand
  1. User will needs to create the Module and Manifest files. Instructions below:

Note: The following steps are to be done on your local laptop

  1. Create a folder named COMPANYNAME.FUNCTIONNAME under C:\Users\$env:USERNAME\Documents\WindowsPowerShell\Modules
    • If Modules folder doesn't exist, create modules folder with following command:
    New-Item -Path "C:\Users\$env:USERNAME\Documents\WindowsPowerShell\Modules" -ItemType Directory
    
  2. Create a file called COMPANYNAME.FUNCTIONNAME.psm1 inside your COMPANYNAME.FUNCTIONNAME folder
  3. Edit your COMPANYNAME.FUNCTIONNAME.psm1 with preferred Powershell editor
    • Code your function(s) into the psm1 file and save file
  4. Open Powershell ISE and change directory to be in the folder from step 1 (COMPANY.FUNCTIONNAME)
  5. Paste the following code block into ISE and edit info like Author, version, etc. Run code block to create the module manifest file
    $RootModule = Get-ChildItem | Where-Object {$_.Name -like "*.psm1"} | Select-Object -ExpandProperty Name 
    $Path = $RootModule -replace ".psm1", ".psd1" 

    $manifest = @{
    Author        = 'Gabe Kerntke' 
    Description   = 'Test Description' 
    FunctionsToExport = @('Show-Calendar', 'Hello-World') 
    Path          = $Path  
    RootModule    = $RootModule   
    CompanyName   = 'COMPANY'    
    LicenseUri    = 'https://opensource.org/licenses/MIT' 
    ModuleVersion = '1.0.0' 
    CmdletsToExport = ''
    AliasesToExport = ''
    }    
    New-ModuleManifest @manifest  
  • Note: You don't need to use both Export-ModuleMember and FunctionsToExport. You only need to use one of those to export your functions. If you have a manifest, then you should be using FunctionsToExport.

  • One nice feature of having a module manifest with FunctionsToExport defined, is that Powershell can auto import your module if you call one of the exported functions. Your module still has to be in the $ENV:PSModulePath variable for this to work. This is why it is important to populate the FunctionsToExport. The default value for this is * to designate that it is exporting all functions defined in the module. This does work, but the auto import functionality depends on this value.

  • Notes about versions: Inside the manifest file for the Powershell module is the "ModuleVersion = '1.0.0'" line. The version needs to be edited before each pipeline run otherwise if the same version has already been published the pipeline will fail out. The NuGet packages in Artifactory are immutable.

Manual method for publishing a Powershell module to your local desktop for testing

Click to expand

To publish a module (testing local):

  1. Install NuGet:
    • Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser
  2. Creating the Powershell repository
    • Register-PSRepository -Name Demo_Nuget_Feed -SourceLocation C:\Users\$env:USERNAME\Downloads\Demo_Nuget -PublishLocation C:\Users\$env:USERNAME\Downloads\Demo_Nuget -InstallationPolicy Trusted
  3. Publish module to repository:
    • Publish-Module -Name Show-Calendar -Repository Demo_Nuget_Feed -Verbose -LicenseUri 'https://opensource.org/licenses/MIT'
  • Note: If issues with resolving the package source, double check your TLS version. Need v1.2 [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12