Simple Powershell code that can be used to immidiately logoff a closed RDP session on a Windows server using windows task scheduler to trigger its run at sessions disconnects.

$sessions = ((quser | Where-Object {$_ -match ‘Disc’}))
foreach ($session in $sessions) {

$sessionid= ($session -split ‘ +’)[2]
logoff $sessionid
}