How to turn screensaver on (windows 7) by a code (in cmd)?
5 Answers
Does the following meet your requirements?
start logon.scr /s
As long as the .scr
is on the PATH the above command should work.
EDIT: I don't know if Windows 7 comes with logon.scr
, make sure you're testing it with a .scr
that is actually installed in Windows 7.
Note that I got the idea of just invoking the .scr
with /s
from Screensaver Sample Command Line Options:
When Windows runs your screensaver, it launches it with one of three command line options:
- /s – Start the screensaver in full-screen mode.
- /c – Show the configuration settings dialog box.
- /p #### – Display a preview of the screensaver using the specified window handle.
EDIT 2:
I did some additional searching and found that you could create lock.cmd
:
@start /wait logon.scr /s & rundll32 user32.dll,LockWorkStation
Or lock.vbs
:
Set objShell = CreateObject("Wscript.Shell")
' The "True" argument will make the script wait for the screensaver to exit
returnVal = objShell.Run("logon.scr", 1, True)
' Then call the lock functionality
objShell.Run "rundll32.exe user32.dll,LockWorkStation"
Neither of these answers is perfect, both reveal a flicker of the desktop after the screen saver is disabled and just prior to the workstation being locked.
It may not be possible to reproduce the system behaviour of starting the screen saver and password protecting on resume. Even the answer to Launch System Screensaver from C# Windows Form only starts the screen saver, it does not password protect on resume.

- 1
- 1

- 25,263
- 7
- 54
- 64
-
i don't remember how that worked in older windows but in 7 it just shows how that screensaver would look like but it doesn't actually turn it on (what i mean is when u interact with system the saver just disappears, when the logon screen should be shown) – o_O Sep 16 '09 at 15:48
-
@Grant, Would you mind copying "Edit 3" into a separate Answer? I'd like to upvote it, but the point of upvoting is so that the correct answer is on top, and folks don't have to sift through the earlier attempts. :-) – system PAUSE Sep 17 '09 at 16:28
-
@system: The C# solution is now a separate answer. I've left this answer as-is in case the `cmd` or `vbs` solutions are "good enough" for others asking the same question. – Grant Wagner Sep 17 '09 at 16:55
Putting together the cmd
and vbs
script ideas with the code from the answer to Launch System Screensaver from C# Windows Form I came up with the following:
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "LockWorkStation")]
private static extern IntPtr LockWorkStation();
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
LockWorkStation();
}
public static void Main()
{
LockDesktop.SetScreenSaverRunning();
}
}
To build it, install the .NET Framework, copy and paste the above code into lock.cs
, then run:
%SystemRoot%\Microsoft.NET\Framework\v3.5\csc.exe lock.cs
Put the created lock.exe
in your path, after that, typing lock
should engage the configured screen saver and lock your workstation.

- 1
- 1

- 25,263
- 7
- 54
- 64
-
1Too sweet! This works perfectly for me on XP, 2003 and 2008, and I can't imagine anything that would make it fail on Vista or 7 (though I haven't tried it there). THANKS! – system PAUSE Sep 17 '09 at 19:30
-
@oO Take Note: Windows 7 ships with .NET 3.5 SP1, so you can skip the installation step, if you try this. – system PAUSE Sep 17 '09 at 19:45
-
@system PAUSE well, it does - after activating there is a blip (screensaver i guess) and then u are redirect to logon screen. there is a fix to it - i posted is as an answer – o_O Sep 18 '09 at 00:13
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
public static void Main()
{
LockDesktop.SetScreenSaverRunning();
}
}
This works - only downside is that u cant interact with pc for something like 7 sec, but i guess its 7's to give ppl time before making screensaver 'permanent'.

- 516
- 2
- 8
- 25
-
Grant Wagners solution starts the screensaver and then locks the screen, but the lock causes the screensaver to exit. This solution is just starting the screensaver without explicitely locking it, so it takes a few seconds before it is locked by windows. Reversing the order of Grant Wagners solution (calling LockWorkStation before SendMessage) fixes both problems, the screen is locked immediately and the screensaver is started after it. – Istador Sep 22 '16 at 14:41
I have Windows 7. I placed the line:
@start /wait %windir%\ExtraPath\ScreenSaverName.scr /s & rundll32 user32.dll,LockWorkStation
in a batch (.bat) file, place it in a appropriate dir, and created a shortcut pointing to this, with the desired shortcut key.
In this line, \ExtraPath
is the additional path under your win dir (usually this is \system32
) where the screen savers are located, and ScreenSaverName.scr is the name of the desired screen saver itself.
It works perfectly.
Now I can press the shortcut keys to run the screen saver and lock the pc.