· Uwe Janke
Commands / Stop-sqmSqlProcess

Stop-sqmSqlProcess

Session Management sqmSQLTool v1.9.90+ · Stop ✓ -WhatIf supported  ·  ⚠ terminates a live connection
Kills one or more SQL Server sessions (SPIDs) via 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

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMEDefault instance of the local computer, or SERVER\INSTANCE.
-SqlCredentialPSCredentialOptional, SQL credential for both the lookup and the kill connection.
-Spidint[]Required, One or more session IDs to terminate. Unknown/already-gone SPIDs are reported as KillStatus = 'NotFound', not an error.
-ReasonstringOptional, Free-text reason, written to the log and included in the owner notification if -NotifyOwner is set.
-NotifyOwnerswitchSwitch$falseSend a WTSSendMessage notification to the session's host after a successful kill. Off by default, so scheduled/automated kills never pop unexpected messages.
-NotifyTimeoutSecondsintOptional30How long the message stays on screen at the recipient before auto-dismissing.
-EnableExceptionswitchSwitch$falseThrow immediately instead of writing an error record and returning $null.
-WhatIf / -ConfirmswitchSwitch, Standard ShouldProcess support (ConfirmImpact 'High') — per SPID, so a batch of kills can be previewed or confirmed one at a time.

Execution Flow

START Read sys.dm_exec_sessions for every -Spid (BEFORE kill) SPID still an active session? NO KillStatus = NotFound logged, next SPID continues YES -WhatIf / Confirm declined? YES → KillStatus = WhatIf, next SPID Stop-DbaProcess -Spid (KILL) failure → KillStatus = Failed, logged, next SPID Success -NotifyOwner set? NO NotifyStatus = Skipped "not requested" YES host_name known for this session? NO NotifyStatus = Skipped no host_name reported YES Send-sqmWtsMessage -ComputerName host_name WTSOpenServer → WTSEnumerateSessions (state=Active only) → WTSSendMessage per active session — no msg.exe involved Delivered? YES NO NotifyStatus = Success NotifyStatus = Failed Add result row · loop to next -Spid

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' -NotifyOwner
Preview only — nothing is killed, nothing is sent
Stop-sqmSqlProcess -SqlInstance SQL01 -Spid 62,71 -WhatIf