In a secure or air-gapped environment, you can’t use Install-Module to download modules directly from the PowerShell Gallery. However, you can use an online machine to download the module and its dependencies, transfer them to the offline machine, and install them manually.
This guide will walk you through the process using the popular Posh-SSH module as an example, but the steps can be applied to any module from the PowerShell Gallery.
The Process: An Overview
The process involves three main steps:
- On an Online Machine: Use the
Save-Modulecmdlet to download the module and all its required dependencies to a local folder. - Transfer: Copy the downloaded module folder to the offline machine using a USB drive or other removable media.
- On the Offline Machine: Place the module folder into one of PowerShell’s module directories, where it will be automatically discovered.
Step 1: Save the Module on an Online Machine
On a Windows machine with an internet connection and PowerShell, follow these steps.
-
Create a directory to save the module.
$repoPath = "$env:USERPROFILE\OfflineModules" New-Item -Path $repoPath -ItemType Directory -Force -
Use
Save-Moduleto download the module and its dependencies. TheSave-Modulecmdlet is designed specifically for this purpose.Save-Module -Name Posh-SSH -Path $repoPathThis command will connect to the PowerShell Gallery, download the
Posh-SSHmodule, and also download any other modules thatPosh-SSHdepends on. -
Verify the download. You should now have a
Posh-SSHfolder inside your$repoPathdirectory, containing the module files.
Step 2: Transfer the Module to the Offline Machine
- Navigate to the folder where you saved the module (
C:\Users\YourUser\OfflineModulesin our example). - You will see a folder named
Posh-SSH. Copy this entire folder to a USB drive or other removable media.
Step 3: Install the Module on the Offline Machine
Now, on your offline machine, you need to place the Posh-SSH folder into a location where PowerShell can find it. PowerShell automatically checks for modules in the directories listed in the $env:PSModulePath environment variable.
Where to Place the Module
You have two main options:
-
For the Current User Only: This is the recommended location for most use cases.
- Path:
$env:USERPROFILE\Documents\PowerShell\Modules(for PowerShell 5.1) or$HOME/.local/share/powershell/Modules(on Linux/macOS). - If the
Modulesdirectory doesn’t exist, you can create it.
- Path:
-
For All Users (System-wide): This requires administrator privileges.
- Path:
$env:ProgramFiles\WindowsPowerShell\Modules(for PowerShell 5.1) or$env:ProgramFiles\PowerShell\Modules(for PowerShell 7+).
- Path:
Installation Steps
- On the offline machine, copy the
Posh-SSHfolder from your USB drive into one of the module paths listed above. For example, for the current user:# Ensure the destination directory exists $userModulePath = "$env:USERPROFILE\Documents\PowerShell\Modules" if (-not (Test-Path $userModulePath)) { New-Item -Path $userModulePath -ItemType Directory -Force } # Copy the module from the USB drive (e.g., D:\) Copy-Item -Path "D:\Posh-SSH" -Destination $userModulePath -Recurse -Force
Step 4: Verify the Installation
After copying the files, you can verify that PowerShell can see the new module.
-
Open a new PowerShell terminal on the offline machine.
-
Check if the module is available:
This command should list theGet-Module -Name Posh-SSH -ListAvailablePosh-SSHmodule and its version. -
Import the module to start using its commands:
Import-Module -Name Posh-SSH -
Verify the commands are available:
You should now see a list of all the commands included in theGet-Command -Module Posh-SSHPosh-SSHmodule, such asNew-SSHSessionandInvoke-SSHCommand.
Conclusion
Installing PowerShell modules in an offline environment is a simple process of using an online machine to download the necessary files and then manually placing them in the correct directory on the offline system.
By using the Save-Module cmdlet, you can ensure that you capture not only the module you want but also all of its dependencies, making the offline installation process smooth and reliable. This method works for any module in the PowerShell Gallery and is an essential skill for managing air-gapped systems.