Is there a way to lock a Windows XP machine via the command line? The shutdown command doesn't have an option for it.
- 9,404
- 9
- 35
- 36
-
1From a Remote Desktop session, you should be able to use CTRL+ALT+END 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.  – bobbymcr Aug 29 '09 at 17:37
-
[psshutdown](https://docs.microsoft.com/en-us/sysinternals/downloads/psshutdown) has a `-l` parameter to lock your workstation. – Marc Durdin Oct 13 '20 at 04:56
11 Answers
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)
- 2,905
- 1
- 19
- 14
-
3For 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
-
3Calling functions like this using rundll32 is really not recommended. For more information, see http://blogs.msdn.com/oldnewthing/archive/2004/01/15/58973.aspx -- scroll a little down to the "rundll32" section. – u1686_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
-
@TuxRug I've included it in the source link. It's from the Futurama movie Bender's Big Score. Nibbler is the poodle-monkey. – Grant Jan 19 '11 at 17:11
-
4
-
-
2@AnthonyW: [`tsdiscon`](https://technet.microsoft.com/en-us/library/bb490805.aspx) seems to log out of the session, not lock it. – pyb Jul 13 '17 at 16:57
-
1I recognize that this is an older question, but I just found that Microsoft has an article documenting the use of rundll32, so this does appear to be the recommended method: https://docs.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/changing-computer-state?view=powershell-5.1 – Aakash Shah Nov 02 '17 at 02:03
-
Works for me! I needed a way to remotely lock my remote workstation that is otherwise unlockable due to [my Computa Pranksta device keeping it awake](https://www.amazon.com/Programmable-Jiggler-50-Settings-Makes-Hacked-Randomly-Gibberish/dp/B06ZYZ2GTB/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=wwwel-20&linkId=fc2cb611974b74ee5226deac2c617932) so it won't sleep (so I can remote in), and the command above works! – Gabriel Staples Jul 24 '18 at 23:42
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);
}
- 501
- 2
- 4
-
1I compiled this with VS2015 using this command: `cmd /s /c ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64 & cl.exe /W4 LockWorkStation.cpp /link /RELEASE /ENTRY:mainStartup /SUBSYSTEM:CONSOLE kernel32.lib user32.lib"` – Kevin Smyth Dec 06 '17 at 20:04
-
Thanks this is fantastic! I think it might just be the smallest Windows EXE I've ever made. Here's a great icon for it https://www.iconarchive.com/show/soft-scraps-icons-by-hopstarter/Lock-Lock-icon.html – rkagerer Jul 12 '20 at 17:50
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.
- 3,451
- 10
- 46
- 65
-
[TSDISCON has been introduced in Windows 2000](http://support.microsoft.com/kb/321705) just checked, and it is available in windows XP as well. – Jeroen Wiert Pluimers Jan 25 '12 at 10:24
-
3[Don't use rundll32.exe with LockWorkStation](http://blogs.msdn.com/b/oldnewthing/archive/2004/01/15/58973.aspx), it will mess with the call stack, and leave that mess in your session. – Jeroen Wiert Pluimers Jan 25 '12 at 11:05
-
1This is the [Wayback Machine](http://web.archive.org/web/20150405214457/http://blogs.msdn.com/b/oldnewthing/archive/2004/01/15/58973.aspx) link for that article. – Guillermo Prandi Jul 29 '20 at 15:00
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
- 41
- 2
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
-
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 03 '15 at 18:37
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()
- 7,984
- 2
- 19
- 32
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
- 635
- 1
- 5
- 9
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.
- 11
- 1
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.
- 18,569
- 15
- 57
- 68
- 1
- 1
-
3This 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
-
1