Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to lock a Windows XP machine via the command line? The shutdown command doesn't have an option for it.

share|improve this question
From a Remote Desktop session, you should be able to use <kbd>CTRL</kbd>+<kbd>ALT</kbd>+<kbd>END</kbd> to show the Windows Security screen (where you can lock your workstation, restart, etc.). In Vista or above, you can also select "Windows Security" from the Start Menu. !Windows Security menu item – bobbymcr Aug 29 '09 at 17:37
up vote 68 down vote accepted
rundll32.exe user32.dll,LockWorkStation

I've been warned that this isn't recommended (except by Microsoft). The warnings are also centered around the command's close relative, ExitWindowsEx (Which shuts down the computer). I've never had any issues with it, but YMMV.

Schlump: The poodle-monkey may be right. The legend warns that the code is powerful and dangerous.
Nudar: My God. We'd better use it only three or four times. Six, max.
Nibbler: But even a single use could shatter the universe!
Nudar: Got it. Two or three times.


share|improve this answer
well that was a quick answer. Thanks! – Herms Aug 11 '09 at 14:31
For a while I had a webpage on my computer that, if visited, would invoke this command (on my computer, not the visitors). It was basically a remote lock for my computer, for when I forget to lock it when I leave the house and realize that my brother is visiting. – Grant Aug 11 '09 at 14:32
Calling functions like this using rundll32 is really not recommended. For more information, see -- scroll a little down to the "rundll32" section. – grawity Aug 12 '09 at 13:05
I've never had a problem with it, and it still works in Windows 7 (one could presume that it wasn't problematic enough to fix). Either way, it's still the only command line option to lock the computer (short of installing a program to do it for you). – Grant Aug 12 '09 at 13:25
Would give you +2 for poodle-monkey if I could. – Jason Sundram Aug 12 '09 at 15:50

If you have access to Visual Studio's C++ compiler here's the (extremely complicated) source:

//Locks the console.
//To compile (VC++ 2003, on one line):
//      cl.exe /W4 LockWorkStation.cpp /link /RELEASE /OPT:REF /OPT:NOWIN98
//                  /ENTRY:mainStartup /SUBSYSTEM:CONSOLE kernel32.lib

#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
    #undef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500 
#include <windows.h> 

void mainStartup(void)
share|improve this answer

Note that in Windows Vista/7, you can use the command tsdiscon to disconnect a Remote Desktop session/lock your workstation.

If you use the rundll32.exe user32.dll, LockWorkStation command in a Remote Desktop session (in Windows 7/Vista), the session will continue, but you will just see the lock screen in the Remote Desktop window.

share|improve this answer
TSDISCON has been introduced in Windows 2000 just checked, and it is available in windows XP as well. – Jeroen Wiert Pluimers Jan 25 '12 at 10:24
Don't use rundll32.exe with LockWorkStation, it will mess with the call stack, and leave that mess in your session. – Jeroen Wiert Pluimers Jan 25 '12 at 11:05

Since it's not recommended to run LockWorkStation via rundll32.exe, another solution is to use Wizmo. Just run:

wizmo lock
share|improve this answer

I set my computer to automatically logon, immediately run "rundll32.exe user32.dll,LockWorkStation" and then start loading apps (single .CMD file in my startup folder).

Works nicely. When I need to reboot and I'm at a breaking point I restart the computer, go on a break, etc and when I get back I unlock my computer. Apps all loaded.

share|improve this answer

For running on a scheduler or after some minutes you leaved computer you can use timeout /t 36000 /nobreak & rundll32.exe user32.dll,LockWorkStation create a .bat file put it in scheduled task, put the trigger run on idle.

You can change /t xxx. how much you need to wait.

Thanks to Kevin, he used the command for shutdown, thats: timeout /t 36000 /nobreak & shutdown /h /f

share|improve this answer
Just so you aren't disappointed if you do not get a response, you should know you have answered a question which has an accepted answer and is 5 years old. You have not done anything wrong. Just wanted you to know. – CharlieRB Aug 3 '15 at 18:37

Here is the working bat command for Remote PC

@echo off
@echo Lock Remote PC
SET /P PC=ıp or Host Name: 
\\%PC%\c$\Windows\System32\rundll32.exe user32.dll,LockWorkStation
share|improve this answer

You can also do this from a local machine to lock a remote workstation by using a UNC path:

\\computername\c$\Windows\System32\rundll32.exe user32.dll,LockWorkStation

Getting access denied with Windows 7 workstations, but works with Windows XP.

share|improve this answer
This will just run the rundll32.exe binary on your local workstation from the remote workstation's disk, and then lock your local workstation. – hjd Sep 17 '12 at 12:09
This locks the machine on which it is running – G-. Nov 19 '13 at 16:51

On Windows Server 2012 press: Win + L

share|improve this answer
This isn't a command-line command. – Daniel Beck Oct 30 '12 at 3:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .