If you work in Ops support you often run into situations where you need to remotely log a user off their machine. Today I’m going to share my general go-to snippet for quickly doing this. To me, quser.exe does everything I need it to and and the logoff command handles the actual logoff once I know the user ID. So instead of reinventing the wheel, we can simply wrap that in a way that flows quickly:

To start, we simply need to run quser.exe in an Invoke-Command scriptblock:

$Computer = Read-Host "Enter a Computer name"

if(Test-Connection -ComputerName $Computer -Count 1 -Quiet){
    Invoke-Command -ComputerName $Computer -ScriptBlock {quser}

    $ID = Read-Host "Enter ID of user to logoff"

    Invoke-Command -ComputerName $Computer -ArgumentList $ID -ScriptBlock {
        param($ID)

        logoff $ID
    }
} else{
    Write-Host "$Computer appears to be offline.`n"
}

Read-Host "Press enter to exit"

So as you can see here we’re just doing a quick ping check based on user input and then displaying the list of current sessions followed by prompting the user to input an ID to force logoff. 

Note: This may require elevation in your environment so be sure to launch the script as admin, or wrap it in a .lnk shortcut that auto launches as admin.

That’s it! Quick and simple way to force users to logoff. What other sort of quick scripts do you run in your environment?