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

link|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
feedback

7 Answers

up vote 23 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.

(Source)

link|improve this answer
well that was a quick answer. Thanks! – Herms Aug 11 '09 at 14:31
1  
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 blogs.msdn.com/oldnewthing/archive/2004/01/15/58973.aspx -- 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
1  
Would give you +2 for poodle-monkey if I could. – Jason Sundram Aug 12 '09 at 15:50
show 2 more comments
feedback

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

//
//LockWorkStation.cpp
//
//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 
#endif
#include <windows.h> 

void mainStartup(void)
{
    LockWorkStation(); 
    ExitProcess(0);
}
link|improve this answer
feedback

Try this command:

rundll32.exe user32.dll, LockWorkStation
link|improve this answer
feedback

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.

link|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 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 at 11:05
feedback

Why do you need this from the command line? Is this something you want for convenience so you can lock the workstation easily when you leave? Win+L will quickly lock the keyboard.

link|improve this answer
Sometimes you want to lock the machine when you aren't at the keyboard. For example: Remotely, Every day at 4:00pm, 5 minutes after you click something, when a file download is done, etc. – Grant Aug 12 '09 at 15:41
Makes sense. The rundll32 option will work, but as pointed out it can be problematic. The best option would be to write a simple C++ or C# program that calls the appropriate function in user32.dll. – Scott Dorman Aug 12 '09 at 16:11
In this case, my coworker needed a machine he uses to refresh his domain credentials (he changed his password), which locking achieves. However, his only way to get to the machine was remote desktop, and that doesn't give you a way to lock. So he asked me about locking via the command line. – Herms Aug 13 '09 at 15:36
feedback

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.

link|improve this answer
feedback

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

wizmo lock
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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