I've setup remote access on my computer (Windows 7 Pro x64 via RDP). The [WOL] wake-on-lan function is already working so I can wake it up from sleeping. When I end a remote desktop session (start-menu -> logout) the computer just stays at the login screen and doesn't go back to sleep.

I'd like to have a batch file (or really something) which I can execute from my remote PC to 1. end (disconnect or logout) the RDP session and 2. put the PC to sleep (it does NOT have to logout the user).

I tried setting up a windows task which puts the computer to sleep 30 seconds after a user logs out (didn't work). I tried putting it to sleep using a batch file already but the session kept running, and I could not do anything and had to hard boot my PC.


End RemoteDesktop Session and Standby

create a batch file (or really something) which i can activate from my remote-pc to end the session and put the pc to sleep (does not have to log out the user)

Two example scripts below:

  1. with a LOGOFF; and,
  2. another with an RDP DISCONNECT (no logoff)

I wasn't sure if you wanted one or the other so I put both below which should work fine from Windows 7 from what I tested.

For the logoff script, I put some if logic to not logoff console, system, or rdp-tpc listening sessions since you're only interesting in RDP connected remote sessions.

For the disconnect script you will just disconnect the session that runs it as needed so no need for additional logic on this one (this is the one you probably need).

The assumption is you will run this as a saved batch script manually and if you decide to use the logoff script rather than the disconnect script, you will have permission to logoff all or any applicable sessions.


(This script will also wait 5 seconds after the disconnect of the current session and then put the PC to sleep.)

Batch Script Example (DISCONNECT all sessions)


::: Disconnected current RDP session

::: Pause 5seconds before going to sleep ensuring enough time 
PING -n 5 > NUL

::: Put computer to sleep
%systemroot%\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0


(This script will also wait 30 seconds after the logoff all the sessions and then put the PC to sleep.)

WARNING: This script will not be able to be run ad hoc by double clicking this batch file for the OP's needs specifically, but I kept this here as a bonus item in case anyone finds use with it otherwise. Since it will logoff the accounts before the sleep logic runs, that sleep logic will not work in that particular case. This would need to be run from Task Scheduler, with Highest Privileges, and Run whether the user is logged on or not—I supposed you could setup this way and NOT on a schedule and just kick off/run as needed from Task Scheduler too. (screen shot below)

Batch Script Example (LOGOFF all sessions)


FOR /F %%S IN (


) DO (

IF NOT %%~S==0 IF NOT %%~S==1 IF NOT %%~S==65536 RWINSTA %%~S


::: Pause 5 seconds before going to sleep ensuring enough time 
PING -n 5 > NUL

::: Put computer to sleep
%systemroot%\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0


Screen Shot Scheduled Task Options

enter image description here

Further Research, Reading, and Sources

  • The second method did not work, since it terminates the script after the logoff (at least i think thats what happens). The first one however worked like a charm,thanks! – Niphram Jan 2 '16 at 9:56
  • I also like the way you used ping to wait 5 seconds, never would have though of that :-) – Niphram Jan 2 '16 at 10:02

Please try this: click a blank spot on the Desktop while in RDP session and press Alt+F4. You'll get the old "power" menu and from there you can force your computer to go to sleep and that will also disconnect your RDP of course.

  • I did not know that, thanks. But i am asking myself why the sleep option is there and not in the normal place. – Niphram Jan 2 '16 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.