batch script to check if anyone is logged in to terminal server and then reboot?

Posted on 2009-04-17
Last Modified: 2013-11-21
I have a .bat file with a simple "shutdown -r" that is run every night at 04:00.
However I wish to change the script so that it checks if anyone is logged in first.
If no users are logged in then continue with the reboot.
If 1 or more users are logged in then it should not reboot.
Question by:bergsprekken
    LVL 82

    Accepted Solution

    Assuming this is exclusively for servers (not XP): you can look for active or disconnected sessions; if none exist, do the reboot.
    As usual, try it on a test server first
    Terminal Server Commands: QUERY

    query session | findstr /i "Active Disc."
    if errorlevel 1 shutdown -r

    Open in new window


    Author Closing Comment

    It works, had to remove the "." in "Active Disc." though.

    query session | findstr /i "Active Disc"
    if errorlevel 1 echo Nobody logged in :o
    if errorlevel 0 echo Dont reboot - people are working..
    LVL 82

    Expert Comment

    Sorry for the ".", had to take this out of the document above.
    And you don't need the last line; "if errorlevel 0" is *always* true. "if errorlevel X" checks if the errorlevel is X *or* *higher*.
    To check for no error, you'd need "if not errorlevel 1", or you can use if ... else, for example like this:

    query session | findstr /i "Active Disc."
    if errorlevel 1 (
      Rebooting in 1 minute, enter 'shutdown -a' to abort ...
      shutdown -r -t 60
    ) else (
      Don't reboot - people are working.

    Open in new window


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
    This is my 3rd article on SCCM in recent weeks, the 1st ( dealing with installat…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now