Install the SQL Server PowerShell module

APPLIES TO: YesSQL Server YesAzure SQL Database YesAzure Synapse Analytics (SQL DW) YesParallel Data Warehouse

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.
  • SQLPS: The SQLPS module is included with the SQL Server installation (for backward compatibility), but is no longer being updated. The most up-to-date PowerShell module is the SqlServer module.

Note

The versions of the SqlServer module in the PowerShell Gallery support versioning and require PowerShell version 5.0 or greater.

For help topics, go to:

SQL Server Management Studio

SQL Server Management Studio (SSMS), beginning with version 17.0, doesn't install either PowerShell module. To use PowerShell with SSMS, install the SqlServer module from the PowerShell Gallery.

Note

With version 16.x of SSMS, an earlier version of the SqlServer module is included with SQL Server Management Studio (SSMS)

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.

You can use the PowerShell extension, which provides rich PowerShell editor support in Azure Data Studio.

Installing or updating the SqlServer module

To install the SqlServer module from the PowerShell Gallery, start a PowerShell session as an administrator. You also start Azure Data Studio as an administrator and run these commands in a PowerShell session in the integrated terminal.

Install the SqlServer module

Run the following command in your PowerShell session to install the SqlServer module for all users:

Install-Module -Name SqlServer

To view the versions of the SqlServer module installed

Execute the following command to see the versions of the SqlServer module that have been installed

Get-Module SqlServer -ListAvailable

Install for the current user rather than as an administrator

If you aren't able to run the PowerShell session as an administrator, install for the current user using the following command:

Install-Module -Name SqlServer -Scope CurrentUser

To overwrite a previous version of the SqlServer module

You can also use the Install-Module command to overwrite a previous version.

Install-Module -Name SqlServer -AllowClobber

Note

PowerShell always uses the latest module 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:

Install-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 version side by side to allow you the ability to experiment with the latest version, yet still have older modules installed.

However, if you don’t want to keep older module versions, then 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:

Get-Module SqlServer -ListAvailable

You can use the following command to remove older versions:

Uninstall-module -Name SQLServer -RequiredVersion "<version number>" -AllowClobber

Troubleshooting

If you run into problems installing, see the Install-Module documentation and Install-Module reference.

Using a specific version of the SqlServer module

To use a specific version of the module, import it with a specific version number similar to the following command:

Import-Module SqlServer -Version 21.1.18080

Pre-release versions of the SqlServer module

Pre-release (or "preview") versions of the SqlServer module may be available in the PowerShell Gallery.

Important

These versions may 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 then open a new session.

To discover pre-release versions of the SqlServer module

To discover the pre-release (preview) versions of the SqlServer module, run the following command:

Find-Module SqlServer -AllowPrerelease

To install a specific pre-release version of the SqlServer module

To install a specific pre-release version of the module, install it with a specific version number.

You can try to use the following command:

Install-Module SqlServer -RequiredVersion 21.1.18040-preview -AllowPrerelease

SQL Server PowerShell on Linux

Visit Manage SQL Server on Linux with PowerShell Core to see how to install SQL Server PowerShell on Linux.