How to Log Off a Terminal Session in Windows: logoff, a Shortcut and a Script

Category: Windows
Posted: September 3, 2011
Author: Antonio

A terminal session should be ended, not left hanging open. The simple logoff command does it in a second, yet both users and administrators tend to forget about it.

logoff shortcut for ending a Windows terminal session

Below are three ways: a desktop shortcut for yourself, a command for ending someone else’s session, and my script that enables a user, lets them work for a set time and then logs them off.

Closing the window is not logging off

Many people just close the Remote Desktop window with the X button. That doesn’t end the session, it only disconnects it: all programs keep running on the server and keep using memory. An administrator can set up automatic logoff after a period of inactivity, but not every server is configured that way.

The logoff command ends all of the user’s processes and deletes the session from the server. If you actually want to disconnect and leave your programs running, use the tsdiscon command instead.

The quick way: a logoff shortcut

When I work on a terminal server, I always make myself a shortcut with the logoff command:

  1. Right-click the desktop → New → Shortcut.
  2. In the location field, type logoff and click Next.
  3. Name the shortcut, for example “Log off”, and click Finish.

Windows fills in the full path to the program by itself. When you’re done working, just double-click the shortcut.

You can also do it without a shortcut: Win+R → logoff → Enter. But a shortcut is easier, especially for an accountant or any other user who won’t remember commands. On an ordinary Windows 7 computer the command works too: it simply signs you out of the current account.

It’s easier on the server as well: if a program isn’t needed, it’s better to close it than to keep it in memory.

How to end another user’s session

First, see which sessions exist on the server:

query session

The output has the columns SESSIONNAME (session name), USERNAME (user), ID (session number) and STATE (Active or Disc for disconnected).

End the session you need by its number:

logoff 3

If the session is on another server, add the server name:

logoff 3 /server:TS01

A few important points:

  • You can always end your own session, but ending someone else’s requires administrator rights (full control over sessions).
  • By name (for example, rdp-tcp#1) you can end only an active session. End a disconnected (Disc) session by its ID.
  • If you specify neither a name nor an ID, logoff ends your own session.
  • You can’t end the console session this way.
  • The user’s unsaved data will be lost, so it’s better to warn them first with the msg command.
msg IvanLogin "Your session will end in one minute"

A script: enable a user, let them work, log them off

For one task I once needed to end a user’s terminal session by user name: enable the account, give the user a set amount of working time, end the session properly and disable the account. I found tips that relied on third-party utilities, but I didn’t want to use them, and the task was more involved than they could handle. So I wrote my own script, with a comment for every command:

@echo off
rem user login
set USR=IvanLogin

rem enable (activate) the account
net user "%USR%" /active:yes

rem how long the user may work, in seconds (2 hours here)
ping -n 7201 -w 1000 -l 1 127.0.0.1 >nul

rem find the session ID: an active session has the ID in column 3,
rem a disconnected one (no session name) has it in column 2
set ID_SES=
for /f "skip=1 tokens=1-3" %%a in ('query session %USR%') do (
  if /i "%%a"=="%USR%" (set ID_SES=%%b) else (set ID_SES=%%c)
)

rem the user never logged on - nothing to end
if not defined ID_SES goto disable

rem warn one minute ahead, then end the session by ID
msg %USR% "Your session will end in one minute"
ping -n 61 -w 1000 -l 1 127.0.0.1 >nul
logoff %ID_SES%

:disable
rem disable the account
net user "%USR%" /active:no

The if not defined check matters: if the user never logged on, the variable stays empty, and logoff without parameters would end the session of whoever ran the script.

Run the script with administrator rights — for example, from Task Scheduler. You don’t need to close the window yourself: when the time is up, the script runs all the commands and closes on its own.

In short

  • Closing the RDP window disconnects you rather than logging you off; your programs keep running.
  • To log yourself off, use a logoff shortcut on the desktop or Win+R → logoff.
  • To end someone else’s session, run query session, then logoff ID; disconnected sessions can be ended only by ID.
Related posts:

Leave a Comment

Copying content from this site is allowed only with a direct link to the source - https://nemcd.com