This appears to work well. It uses the olde ‘rwinsta’ command to work around some Powershell oddities.
# Get list of disconnected RDP sessions $RDPDiscSessions = Get-RDUserSession | Where-Object SessionState -eq STATE_DISCONNECTED # Disconnect each of them one by one foreach ($row in $RDPDiscSessions) { 'Logging off ' + $row.SessionID rwinsta $row.SessionID }