Dela via


sqlps Utility

The sqlps utility starts a PowerShell session with the SQL Server PowerShell provider and cmdlets loaded and registered. You can enter PowerShell commands or scripts that use the SQL Server PowerShell components to work with instances of SQL Server and their objects.

Syntax

sqlps 
[ [ [ -NoLogo ][ -NoExit ][ -NoProfile ]
    [ -OutPutFormat { Text | XML } ] [ -InPutFormat { Text | XML } ]
  ]
  [ -Command { -
             | script_block [ -args argument_array ]
             | string [ command_parameters ]
             }
  ]
]
[ -? | -Help ]

Arguments

  • -NoLogo
    Specifies that sqlps hide the copyright banner when it starts.

  • -NoExit
    Specifies that sqlps continue running after the startup commands have completed.

  • -NoProfile
    Specifies that sqlps not load a user profile. User profiles record commonly used aliases, functions, and variables for use across PowerShell sessions.

  • -OutPutFormat { Text | XML }
    Specifies that the sqlps output be formatted as either text strings (Text) or in a serialized CLIXML format (XML).

  • -InPutFormat { Text | XML }
    Specifies that input to sqlps is formatted as either text strings (Text) or in a serialized CLIXML format (XML).

  • -Command
    Specifies the command for sqlps to run. The sqlps utility runs the command and then exits, unless -NoExit is also specified. Do not specify any other switches after -Command, they will be read as command parameters.

  • -
    -Command- specifies that sqlps read the input from the standard input.

  • script_block [ -argsargument_array ]
    Specifies a block of PowerShell commands to run, the block must be enclosed in braces: {}. Script_block can only be specified when sqlps is called from either PowerShell or sqlps. The argument_array is an array of PowerShell variables containing the arguments for the PowerShell commands in the script_block.

  • string [ command_parameters ]
    Specifies a string that contains the PowerShell commands to be run. Use the format "&{command}". The quotation marks indicate a string, and the invoke operator (&) causes sqlps to run the command.

  • [ -? | -Help ]
    Shows the syntax summary of sqlps options.

Remarks

The sqlps utility starts the PowerShell environment (PowerShell.exe) with the SQL Server PowerShell snap-ins loaded and registered. The SQL Server PowerShell snap-ins are as follows:

  • Microsoft.SqlServer.Management.PSProvider.dll

    Implements the SQL Server PowerShell provider and associated cmdlets such as Encode-SqlName and Decode-SqlName.

  • Microsoft.SqlServer.Management.PSSnapin.dll

    Implements the Invoke-Sqlcmd cmdlet.

You can use sqlps to do the following:

  • Interactively run PowerShell commands.

  • Run PowerShell script files.

  • Run SQL Server cmdlets.

  • Use the SQL Server provider paths to navigate through the hierarchy of SQL Server objects.

By default, sqlps runs with the scripting execution policy set to Restricted. This prevents running any PowerShell scripts. You can use the Set-ExecutionPolicy cmdlet to enable running signed scripts, or any scripts. Only run scripts from trusted sources, and secure all input and output files by using the appropriate NTFS permissions. For more information about enabling PowerShell scripts, see Running Windows PowerShell Scripts.

Examples

A. Run sqlps in default, interactive mode without the copyright banner

sqlps -NoLogo

B. Run a SQL Server PowerShell script from the command prompt

sqlps -Command "&{.\MyFolder.MyScript.ps1}"

C. Run a SQL Server PowerShell script from the command prompt, and keep running after the script completes

sqlps -NoExit -Command "&{.\MyFolder.MyScript.ps1}"