This article provides directions for installing the SqlServer PowerShell module.
PowerShell modules for SQL Server
There are two SQL Server PowerShell modules:
SqlServer: The SqlServer module includes new cmdlets to support the latest SQL features. The module also contains updated versions of the cmdlets in SQLPS. To download the SqlServer module, go to SqlServer module in the PowerShell Gallery.
Note
The versions of the SqlServer module in the PowerShell Gallery support versioning and require PowerShell version 5.1 or greater.
SQLPS: The SQLPS is the module used by SQL Agent to run agent jobs in agent job steps using the PowerShell subsystem.
In the context of SqlServer and SQLPS modules, it is possible for certain command names to exist in both modules.
This can create a conflict when one module is already installed, and the other is being installed or updated. The -AllowClobber parameter can be used to prevent this conflict when installing or updating the module. Additionally, when importing one module, removing the other module first or using the -DisableNameChecking parameter to prevent an error is necessary. These measures ensure that the modules can coexist without causing any issues.
SQL Server Management Studio (SSMS) 16.x (and earlier versions) included the SQLPS module.
Azure Data Studio
Azure Data Studio doesn't install either PowerShell module. To use PowerShell with Azure Data Studio, install the SqlServer module from the PowerShell Gallery.
To install the SqlServer module from the PowerShell Gallery, start a PowerShell session and run Install-Module SQLServer.
PowerShell
Install-Module -Name SqlServer
If running on Windows PowerShell, you can use Install-Module SQLServer -Scope CurrentUser to install the module for the current user and avoid needing elevated permissions.
Install the SqlServer module for all users
To install the SqlServer module for all users, run the command below in an elevated PowerShell session; start a PowerShell session as administrator:
PowerShell
Install-Module -Name SqlServer
Install the SqlServer module to an offline computer
This section walks you through the steps of installing the SqlServer PowerShell module on a computer without internet access. You need two computers to accomplish this installation: a computer connected to the internet and an offline destination computer.
On a computer connected to the internet, download the SQLServer PowerShell module. This command downloads and saves all the files for the module in the sample $env:TEMP\SQLServer\<SomeVersion> folder, defined in the -Path parameter. You can choose a folder of your preference as a destination on your internet-connected computer.
PowerShell
Save-Module -Name SqlServer -Path$env:TEMP
Browse to the folder location in File Explorer to verify that a $env:TEMP\SQLServer\<SomeVersion> folder exists or run this command:
PowerShell
Get-ChildItem -Path$env:TEMP\SQLServer\
Copy the $env:TEMP\SQLServer\<SomeVersion> folder you found in step 2 to the destination offline computer in %ProgramFiles%\WindowsPowerShell\Modules\SqlServer folder (this folder would work for both PS5 and PS7). Be sure to replace <SomeVersion> with the value you found in the previous step.
Confirm that the SqlServer PowerShell module is available on the offline computer by running the following PowerShell cmdlet:
PowerShell
Get-Module SqlServer -ListAvailable
The output might look like this (the actual version might be different and must match <SomeVersion> listed previously):
View the versions of the SqlServer module installed
Execute the following command to see the installed versions of the SqlServer module.
PowerShell
Get-Module SqlServer -ListAvailable
To view the version of the SqlServer module loaded in the current session
PowerShell
(Get-Module SqlServer).Version
Overwrite a previous version of the SqlServer module
You can also use the Install-Module command to overwrite a previous version.
PowerShell
Install-Module -Name SqlServer -AllowClobber
Note
PowerShell always uses the latest module that has been installed.
Update the installed version of the SqlServer module
When updated versions of the SqlServer module are available, you can install the newer version using the following command:
PowerShell
Update-Module -Name SqlServer -AllowClobber
You can use the Update-Module command to install the newest version of the SQLServer PowerShell module, but that doesn't remove older versions. It installs the newer versions side by side to allow you to experiment with the latest version while still having older modules installed.
However, if you don't want to keep older module versions, you can use the Uninstall-Module command to remove previous versions.
You can use the following command to list if more than one version is installed:
PowerShell
Get-Module SqlServer -ListAvailable
You can use the following command to remove older versions:
To use a specific version of the module, import it with a specific version number similar to the following command:
PowerShell
Import-Module SqlServer -Version21.1.18218
Prerelease versions of the SqlServer module
Prerelease (or "preview") versions of the SqlServer module might be available in the PowerShell Gallery.
Important
These versions might be discovered and installed by using the updated Find-Module and Install-Module cmdlets that are part of the PowerShellGet module by passing the -AllowPrerelease switch. To use these cmdlets, install the PowerShellGet module and open a new session.
Discover prerelease versions of the SqlServer module
To discover the prerelease (preview) versions of the SqlServer module, run the following command:
PowerShell
Find-Module SqlServer -AllowPrerelease
Install a specific prerelease version of the SqlServer module
To install a specific prerelease version of the module, install it with a specific version number.
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.