78

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

Herms
  • 9,992

11 Answers11

117
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)

Grant
  • 2,965
15

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);
}
devstuff
  • 501
6

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.

palswim
  • 3,601
5

The Wizmo tool recommended in other answer is pretty old and not being updated / maintenanced anymore.

I suggest using the NirCmd tool instead. It's a lightweight and versatile command-line utility for Windows that allows you to do a ton of pretty cool and useful tasks without displaying any user interface.

To lock the computer, just run:

nircmd.exe lockws
oogami
  • 71
  • 1
  • 2
4

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

wizmo lock
laurent
  • 5,774
3

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

CharlieRB
  • 23,021
  • 6
  • 60
  • 107
Navid
  • 31
1

Use Powershell and Windows API:

$code = @'
[DllImport("user32.dll")]
public static extern void LockWorkStation();
'@
$winApi = Add-Type -MemberDefinition $code -Name WinAPI -Namespace Extern -PassThru
$winApi::LockWorkStation()
wasif
  • 9,176
1

Here is the working bat command for Remote PC

@echo off
COLOR 3E
@echo Lock Remote PC
SET /P PC=ıp or Host Name: 
\\%PC%\c$\Windows\System32\rundll32.exe user32.dll,LockWorkStation
serdar
  • 655
1

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.

Tom
  • 11
-3

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.

Gareth
  • 19,080
-7

On Windows Server 2012 press: Win + L

Andrea
  • 1