Kill all RDP Sessions

If you want to reset all Terminal Server (RDP) Sessions from command line, you can use this PowerShell script.



$ServerName = "localhost"

# Run the qwinsta.exe and parse the output
$queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace \s+,,))} | ConvertFrom-Csv)

# Pull the session information from each instance
ForEach ($queryResult in $queryResults) {
 $RDPUser = $queryResult.USERNAME
 $sessionType = $queryResult.SESSIONNAME
 $sessionID = $queryResult.ID
 # We only want to display where a “person” is logged in. Otherwise unused sessions show up as USERNAME as a number
 If (($RDPUser -match [a-z]) -and ($RDPUser -ne $NULL)) {
  # When running interactively, uncomment the Write-Host line below to show the output to screen
  # Write-Host $RDPUser logged in on Session ID: $sessionID
  $command = "rwinsta.exe $($sessionID)"
  # Write-Host $command 
  iex $command
 }
}
Share on Google Plus

About Vittorio Pavesi

    Blogger Comment
    Facebook Comment

0 comments: