Sql Server Powershell Snapin

Might as well show you the code to load the Sql Server snapin for powershell as well.  I have this in my $profile, as I use the snapin constantly.

function LoadSQLSnapin()
{
 
#
# Add the SQL Server Provider.
#
 
$ErrorActionPreference = "Stop"
 
$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"
 
if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
{
   throw "SQL Server Provider for Windows PowerShell is not installed."
}
else
{
   $item = Get-ItemProperty $sqlpsreg
   $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
}
 
 
#
# Set mandatory variables for the SQL Server provider
#
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000
 
#
# Load the snapins, type data, format data
#
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml
Pop-Location
 
}
 
LoadSQLSnapin
 
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.