windowsbatch-filewindows-10windows-screensaver

How to have window's batch file detect when the screensaver is on?


How to program a batch program to determine if a the screensaver is running or if the user went to sleep, or locked the computer?

In bash in Ubuntu Linux I am using the code: /gnome-screensaver-command -q | grep "is active" to determine if screensaver is running.

Note: I am not seeking recommendations for books, tools, software libraries...

Unless there is a better approach, I am working off of this script to look for processes running. But I have to find the name of the screensaver process.

@echo off
set pn=%1
echo looking for %pn%
tasklist /FI "IMAGENAME eq %pn%" 2>NUL | find /I /N "%pn%">NUL
if %ERRORLEVEL%==0 (
    echo Found program running
) else (
    echo NOT FOUND running  
)

Solution

  • The name of the screensaver process will vary by which screensaver is running, but it should always end in .scr.

    tasklist | find ".scr" >nul
    if %errorlevel% EQU 0 (
        echo Screensaver is running
    ) else (
        echo Screensaver is not running
    )
    

    The default Windows 10 screensavers have the following process names:

    3D Text - ssText3d.scr
    Blank   - scrnsave.scr
    Bubbles - Bubbles.scr
    Mystify - Mystify.scr
    Photos  - PhotoScreensaver.scr
    Ribbons - Ribbons.scr
    

    When the computer is locked, the process LogonUI.exe runs.

    tasklist | find "LogonUI.exe"
    if %errorlevel% EQU 0 (
        echo Computer is locked
    ) else (
        echo Computer is unlocked
    )