PowerShell by Example: Package Management
Managing external dependencies with this code example demonstrating module discovery with Find-Module, installation from repositories, repository registration, and version control of installed packages.
Code
# Register a custom repository (e.g., internal NuGet)
Register-PSRepository -Name "InternalRepo" -SourceLocation "https://nuget.example.com/api/v2" -InstallationPolicy Trusted
# Find a module in the gallery
$module = Find-Module -Name "PowerShellGet" -Repository PSGallery
Write-Host "Found version: $($module.Version)"
# Install a module for the current user (no admin rights needed)
Install-Module -Name "Pester" -Scope CurrentUser -Force -SkipPublisherCheck
# List installed packages
Get-Package -ProviderName PowerShellGet
# Update a module
Update-Module -Name "Pester"
# Uninstall a module
Uninstall-Module -Name "Pester"
# Save module for offline installation
Save-Module -Name "Az" -Path "C:\OfflineModules"Explanation
PowerShell Package Management (formerly OneGet) provides a unified interface for discovering, installing, and managing software packages from various sources. The PowerShellGet module acts as the primary provider for PowerShell modules, scripts, and DSC resources, connecting to repositories like the public PowerShell Gallery or private NuGet feeds. This ecosystem allows developers to easily share and consume reusable code, mirroring the package management experiences found in other languages like Python (pip) or Node.js (npm).
The core lifecycle involves Find-Module to search repositories based on names, tags, or commands; Install-Module to download and register the module on the local system; and Get-Package (or Get-InstalledModule) to inventory installed components. Modules can be installed in two scopes: AllUsers (requiring administrator privileges, installing to $env:ProgramFiles) or CurrentUser (no privileges required, installing to $home). The -Force parameter can override version conflicts, while -SaveModule allows downloading packages without installing them, facilitating offline deployments or manual inspection.
Security and trust are managed through repository policies. By default, the PowerShell Gallery is an untrusted source, requiring user confirmation for installation. The Set-PSRepository cmdlet can modify the InstallationPolicy to 'Trusted', suppressing prompts for automated workflows. Private repositories can be registered using Register-PSRepository, enabling organizations to host proprietary modules securely while using the same standard toolset for dependency management.
- Register private repositories for internal module sharing
- Use
Find-Moduleto discover new tools and resources - Install modules to
CurrentUserscope to avoid admin rights - Save modules for offline inspection or air-gapped installation
Code Breakdown
Register-PSRepository adds a new package source location.Find-Module searches registered repositories for matching packages.-Scope CurrentUser installs module to user profile without admin rights.Save-Module downloads package contents for offline use or inspection.
