Getting Started with PowerShell for SQL Server

By default SQL Server 2008  installs  PowerShell and two SQL Server PowerShell snap-ins which expose SQL Server functionality from PowerShell. PowerShell Snap-ins are simply .NET assemblies which contain Windows PowerShell providers and/or  PowerShell cmdlets that extend the functionality of the shell. When a snap-in is loaded into  PowerShell , its cmdlets and providers  are registered with the shell.

Starting PowerShell

PowerShell can be invoked and used on  SQL Server 2008 server in four ways:

  1. Using the sqlps utility
    The sqlps utility is a command line utility for executing PowerShell commands and  is installed by default in the  \Program Files\Microsoft SQL Server\100\Tools\Binn folder. Since the Binn folder is added to the System PATH variable, the sqlps utility can be launched by selecting Start > Run and then entering sqlps. This should launch the sql utility:
    sql server powershell
  2. PowerShell can also be launched from within  SSMS (SQL Server Management Studio) by right-clicking  an object in the Object Explorer, as shown below. SSMS will then launch launch  sqlps and set
    the location to the object  which you right clicked.
    sql server powershell
  3. PowerShell cmdlets and scripts  can also be launched from inside a SQL Server Agent job. A SQL Server Agent job step can be set to type PowerShell and then  PowerShell cmdlets and scripts can be used.
    powershell sql server
  4. Launch Windows PowerShell and then manually add the SQL Server PowerShell provider functionality. Launch PowerShell from the Start menu (Start > PowerShell) then execute the below commands:
    Get-PSSnapin –registered
    Add-PSSnapin SqlServerProviderSnapin100
    Add-PSSnapin SqlServerCmdletSnapin100

    powershell sql server

    The Get-PSSnapin –registered command simply shows the listing of the available snap-ins and the two Add commands registers the snap-ins.To confirm the snap-ins are added to your PowerShell session execute the command:

    Get-PSSnapin Sql*

Navigate The SQL Server Object Hierarchy using PowerShell

To see the SQLSERVER drive  along with other PowerShell drives which are available in the current environment, execute the cmdlet Get-PSDrive :

sql server powershell


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>