PowerShell: import the SqlServer or SQLPS module
Load the SqlServer module when available, otherwise fall back to the older SQLPS module.
Depending on the machine, the SQL Server cmdlets come from the modern SqlServer module or from the older SQLPS module. This snippet loads SqlServer when it is installed and falls back to SQLPS otherwise.
If (Get-Module SQLServer -ListAvailable)
{
Write-Verbose "Preferred SQLServer module found"
$module = Get-Module SQLServer -ListAvailable
}
else
{
Write-Verbose "SQLServer module not found, using older SQLPS module"
$module = Get-Module SQLPS -ListAvailable
}
Import-Module $module -WarningAction SilentlyContinue -ErrorAction Stop
Notes
-WarningAction SilentlyContinuehides the unapproved-verb warning shown when importingSQLPS.-ErrorAction Stopstops the script if neither module is available.