dbatools is a free PowerShell module with over 180 SQL Server administration, best practice and migration commands included.


Stop-DbaProcess

This command kills all spids associated with a spid, login, host, program or database. If you are attempting to kill your own login sessions, the process performing the kills will be skipped.

Note: this command contains a ton of auto-populated parameters that don’t appear on Get-Help (yet). They’re documented, but dynamic parameters don’t show in help. We’re working on that. Until then, here are the auto-populated parameters you can pass.

  • .PARAMETER Spids
    This parameter is auto-populated from -SqlServer. You can specify one or more Spids to be killed.

  • .PARAMETER Logins
    This parameter is auto-populated from-SqlServer and allows only login names that have active processes. You can specify one or more logins whose processes will be killed.

  • .PARAMETER Hosts
    This parameter is auto-populated from -SqlServer and allows only host names that have active processes. You can specify one or more Hosts whose processes will be killed.

  • .PARAMETER Programs
    This parameter is auto-populated from -SqlServer and allows only program names that have active processes. You can specify one or more Programs whose processes will be killed.

  • .PARAMETER Databases
    This parameter is auto-populated from -SqlServer and allows only database names that have active processes. You can specify one or more Databases whose processes will be killed.

  • .PARAMETER Exclude
    This parameter is auto-populated from -SqlServer. You can specify one or more Spids to exclude from being killed (goes well with Logins).

    Exclude is the last filter to run, so even if a Spid matches, for example, Hosts, if it’s listed in Exclude it wil be excluded.

Screenshots

stop-dbaprocess

Examples

To find all processes for base\ctrlb and sa on sqlserver2014a, then kills them. Uses Windows Authentication to login to sqlserver2014a.

Stop-DbaProcess -SqlServer sqlserver2014a -Logins base\ctrlb, sa

To find processes for spid 56 and 57, then kills them. Uses alternative (SQL or Windows) credentials to login to sqlserver2014a.

Stop-DbaProcess -SqlServer sqlserver2014a -SqlCredential $credential -Spids 56, 77

To find processes that were created in Microsoft SQL Server Management Studio, then kills them.

Stop-DbaProcess -SqlServer sqlserver2014a -Programs ‘Microsoft SQL Server Management Studio’

To find processes that were initiated by hosts (computers/clients) workstationx and server 1000, then kills them.

Stop-DbaProcess -SqlServer sqlserver2014a -Hosts workstationx, server100

To show what would happen if the command were executed.

Stop-DbaProcess -SqlServer sqlserver2014a -Databases master -WhatIf

Get-Help

From PowerShell, execute Get-Help Stop-DbaProcess -Detailed for more information on this function.

Source Code

Want to see the source code? View Stop-DbaProcess.ps1 on GitHub
 

Related commands