Бөлісу құралы:


Install SQL Server PowerShell module

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.

For help articles, go to:

Note

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)

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

Note

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.

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

Install or update the SqlServer module

To install the SqlServer module from the PowerShell Gallery, start a PowerShell session and run Install-Module SQLServer.

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:

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.

  1. 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.

    Save-Module -Name SqlServer -Path $env:TEMP
    
  2. Browse to the folder location in File Explorer to verify that a $env:TEMP\SQLServer\<SomeVersion> folder exists or run this command:

    Get-ChildItem -Path $env:TEMP\SQLServer\
    
  3. 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.

  4. Confirm that the SqlServer PowerShell module is available on the offline computer by running the following PowerShell cmdlet:

    Get-Module SqlServer -ListAvailable
    

    The output might look like this (the actual version might be different and must match <SomeVersion> listed previously):

       PS C:\Users\user1> Get-Module SqlServer -ListAvailable
    
           Directory: C:\Program Files\WindowsPowerShell\Modules
    
       ModuleType Version    Name                     ExportedCommands
       ---------- -------    ----                     ----------------
       Script     22.1.1     SqlServer                {Add-RoleMember, Add-SqlAvail...
    

View the versions of the SqlServer module installed

Execute the following command to see the installed versions of the SqlServer module.

Get-Module SqlServer -ListAvailable

To view the version of the SqlServer module loaded in the current session

(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.

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:

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:

Get-Module SqlServer -ListAvailable

You can use the following command to remove older versions:

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

Troubleshoot

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

Use 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.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:

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.

You can try to use the following command:

Install-Module SqlServer -RequiredVersion 21.1.18218-preview -AllowPrerelease

SQL Server PowerShell on Linux

Visit /sql/linux/sql-server-linux-manage-powershell-core to see how to install SQL Server PowerShell on Linux.

Other modules

  • Az.Sql - SQL service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell.

  • SqlServerDsc - Module with DSC resources for deploying and configuring Microsoft SQL Server.

Cmdlet reference