Most popular PowerShell modules are installed online from the official PowerShell Gallery (PSGallery
) repository using the Install-Module command. However, you won’t be able to install a PowerShell module if your computer is in an isolated network or direct access to PSRepository is restricted. The same is true when you are trying to install the PowerShell module on Windows Server hosts, which usually have direct Internet access blocked. In this article, we’ll show how to install PowerShell modules offline and to import a module from a remote computer (for example, when using the SQLServer management PoSh module).
Note that there are no direct links to download modules from PowershellGallery.com. The only thing you can download from the site is the NuGet package (a .nupkg file). You can install a PowerShell module from the NUPKG file, but this will not be a complete module installation (the main problem is that the dependencies are not resolved).
How to Install PowerShell Modules on Offline Computers?
First of all, install the PowerShell module you need on a computer with Internet access.
$PSVersionTable.PSVersion
Make sure that the module exists in PSGallery:
Find-Module –Name *SqlServer*| Select Name, Version, Repository
Download the module to the specified local folder on your computer:
Save-Module –Name SqlServer –Path C:\PS\
Copy the folder to another computer you want to install the module on.
Let’s see what folders PowerShell modules are stored in:
$env:PSModulePath -split ";"
As you can see, PowerShell modules are located on one of the following paths:
C:\Users\username\Documents\WindowsPowerShell\Modules
($Home\Documents\PowerShell\Modules
) – PowerShell modules in this folder are available only to this user (CurrentUser);C:\Program Files\WindowsPowerShell\Modules
($Env:ProgramFiles\WindowsPowerShell\Modules
) — the path is used to install a module for all computer users (-Scope AllUsers
);C:\Windows\system32\WindowsPowerShell\v1.0\Modules
– default folder for built-in modules.
Copy the module to C:\Program Files\WindowsPowerShell\Modules.
Make sure that the SQLServer module is now available:
Get-Module -Name SQLServer -ListAvailable
You can get the module directory as shown below:
(Get-Module -ListAvailable SQLServer).path
Display the list of commands available in the module:
Get-Command -Module SQLServer
In the same way, you can install any module. I often use this method to install SQLServer, PSWindowsUpdate, or PowerCLI for VMware modules.
Import PowerShell Module from a Remote Computer
If you don’t want to install a PowerShell module on all computers, you can import any module from a remote computer using PSRemoting:
$session = New-PSSession -ComputerName dub-sql1
To display a list of modules installed on a remote computer:
Get-Module -PSSession $session –ListAvailable
To import the specified PowerShell module to your computer:
Import-Module -PSsession $session -Name SqlServer
Don’t forget to close the session when you finish:
Remove-PSSession $session
Another interesting way to locally use a PowerShell module installed on a remote computer via Implicit remoting.
Connect to a remote computer using the Invoke-Command and import the PowerShell module you want:
$session = New-PSSession -ComputerName dub-sql1
Invoke-Command {Import-Module SqlServer} -Session $session
Export module cmdlets from the remote session to the local module:
Export-PSSession -Session $s -CommandName *-Sql* -OutputModule RemoteSQLServer -AllowClobber
The command creates a new RemoteSQLServer PowerShell module on your computer (in C:\Program Files\WindowsPowerShell\Modules). The cmdlet files themselves are not copied.
Close the session:
Remove-PSSession $session
Then to use PowerShell cmdlets from this module, you just need to import them into the session:
Import-Module RemoteSQLServer
All SQL module cmdlets will be available without establishing an explicit connection to the remote computer. Try to query the MS SQL database using the Invoke-Sqlcmd
command. All MSSQL commands are available until you close your PowerShell console or remove the module.
1 comment
Bravo. Excellent blog. Clear, concise and thorough instructions that leave me the viewer in no doubt that you know your stuff. I’ve read a number of guides on the topic of offline (or isolated) computers and installing PowerShell Modules, but I know this one is the best. I’m copying this link like no one’s business. Thanks very much for taking the time to document this!