петък, 17 септември 2021 г.

Powershell Script: Log off all users on a Windows Server or Remote Desktop Server

Here is a quick and nasty powershell script that will log off all users on a Windows Server. In my case, this was called by a scheduled task, once a night, to log off any sessions that were left running on a Windows Server 2016 Remote Desktop Server farm.

$sessions = query session | ?{ $_ -notmatch ‘^ SESSIONNAME’ } | %{
$userID = “” | Select “Id”
$userID = $_.Substring(39,9).Trim()
$userID
}
foreach ($session in $sessions)
{
logoff $session.Id
}