Stop-sqmSqlProcess
Session Management
sqmSQLTool v1.9.90+ · Stop
✓ -WhatIf supported · ⚠ terminates a live connection
Stop-DbaProcess, after first capturing login_name, host_name, program_name and database from sys.dm_exec_sessions — that lookup has to happen before the kill, since the row is gone from the DMV the instant the session ends. With -NotifyOwner, it then pops a message on the affected workstation so whoever was running that query/connection knows it was terminated deliberately, and optionally why.
ⓘ The notification does not shell out to
msg.exe. That classic "net send" successor turned out to be missing entirely on Windows 11 Home and on at least one lab SQL Server box tested during development — it's an RDS-admin-tools component, not guaranteed on client SKUs. Instead, Send-sqmWtsMessage (an internal helper, not separately exported) calls the underlying WTSSendMessage API in wtsapi32.dll directly via P/Invoke, which ships with every Windows installation, server or client. Verified live, cross-machine, in a workgroup lab with no AD domain: a real SQL session opened on one box was killed from another, and the owning workstation received the message.
⚠
host_name in sys.dm_exec_sessions is a value the client reports at connect time — not verified by SQL Server, and trivially wrong or absent for connections routed through application servers, connection poolers, or service accounts. If nobody is logged in interactively on that host (or the name doesn't resolve), the notification is skipped with NotifyStatus = 'Skipped' and a logged reason — it never blocks or fails the kill itself. A delivery failure (host unreachable, no rights, no active session) is likewise recorded as NotifyStatus = 'Failed' rather than thrown.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Default instance of the local computer, or SERVER\INSTANCE. |
| -SqlCredential | PSCredential | Optional | , | SQL credential for both the lookup and the kill connection. |
| -Spid | int[] | Required | , | One or more session IDs to terminate. Unknown/already-gone SPIDs are reported as KillStatus = 'NotFound', not an error. |
| -Reason | string | Optional | , | Free-text reason, written to the log and included in the owner notification if -NotifyOwner is set. |
| -NotifyOwner | switch | Switch | $false | Send a WTSSendMessage notification to the session's host after a successful kill. Off by default, so scheduled/automated kills never pop unexpected messages. |
| -NotifyTimeoutSeconds | int | Optional | 30 | How long the message stays on screen at the recipient before auto-dismissing. |
| -EnableException | switch | Switch | $false | Throw immediately instead of writing an error record and returning $null. |
| -WhatIf / -Confirm | switch | Switch | , | Standard ShouldProcess support (ConfirmImpact 'High') — per SPID, so a batch of kills can be previewed or confirmed one at a time. |
Execution Flow
Return Value
One
PSCustomObject per requested SPID: Spid, LoginName, HostName, DatabaseName, KillStatus (Success / Failed / NotFound / WhatIf), NotifyStatus (Success / Failed / Skipped), Message.
Examples
Kill one SPID, no notification
Stop-sqmSqlProcess -SqlInstance SQL01 -Spid 62
Kill and notify the owner with a reason
Stop-sqmSqlProcess -SqlInstance SQL01 -Spid 62 -Reason 'Blocking the nightly close' -NotifyOwner
Resolve an entire blocking chain and clear it in one call
$chain = Get-sqmBlockingReport -SqlInstance SQL01
Stop-sqmSqlProcess -SqlInstance SQL01 -Spid $chain.BlockedSessions.BlockedSpid `
-Reason 'Blocking chain cleared' -NotifyOwnerPreview only — nothing is killed, nothing is sent
Stop-sqmSqlProcess -SqlInstance SQL01 -Spid 62,71 -WhatIf