Skip to main content

How to Lock the Workstation in a Batch Script

Locking your workstation is a fundamental security practice, protecting your session from unauthorized access when you step away from your computer. While the Win + L keyboard shortcut is the standard manual way to do this, you can also trigger a lock programmatically from a batch script. This is useful for creating custom shortcuts, for IT support scripts, or for forcing a lock at the end of an automated task.

This guide will teach you the simple and direct method for locking a Windows workstation using the built-in RUNDLL32.EXE utility to call a function from a system library.

The Core Command: RUNDLL32.EXE

The RUNDLL32.EXE utility is a standard Windows program whose purpose is to run a function from within a .dll (Dynamic Link Library) file. Many of Windows' graphical and shell functions are not standalone executables but are instead functions inside DLLs. RUNDLL32.EXE is our command-line gateway to them.

To lock the workstation, we need to call the LockWorkStation function, which resides in the user32.dll library.

Syntax: RUNDLL32.EXE user32.dll,LockWorkStation

This single, simple command is the entire solution.

Let's break down the command:

  • RUNDLL32.EXE: The program we are running.
  • user32.dll: The first argument, specifying the DLL file that contains the function we want to call. This is a core Windows library responsible for user interface elements.
  • ,LockWorkStation: The second argument, separated from the DLL name by a comma (with no space). This is the exact, case-sensitive name of the function inside user32.dll that we want to execute.

When this command is run, it has the exact same effect as pressing Win + L. The screen will immediately show the lock screen, and the user will need to enter their password to resume their session.

Basic Example: A Simple "Lock" Script

This is the simplest possible script. You can save it as Lock.bat and place it on the desktop to create a one-click icon for locking the computer.

@ECHO OFF
REM This script locks the workstation.

ECHO Locking the workstation now...
RUNDLL32.EXE user32.dll,LockWorkStation

When you run this, the "Locking..." message will flash on the screen for a fraction of a second, and the workstation will immediately lock. The batch script terminates successfully in the background.

Common Pitfalls and How to Solve Them

The RUNDLL32.EXE user32.dll,LockWorkStation command is extremely reliable and has very few pitfalls.

  • Administrator Rights: This is a key advantage. Locking your own workstation is a standard user privilege. This command does not require administrator rights to run.

  • Syntax Errors: The most common mistake is to add a space after the comma.

    • WRONG: user32.dll, LockWorkStation
    • RIGHT: user32.dll,LockWorkStation The command will fail silently or with a cryptic error if there is a space.
  • What about logoff? The logoff command is completely different. logoff terminates all your running applications and ends your session. LockWorkStation simply locks the screen; all your applications continue to run in the background, and your session remains active.

Practical Example: A "Work Complete and Lock" Script

This script performs a long, automated task (like a backup or a file render) and then automatically locks the workstation when it's finished. This is perfect for end-of-day tasks, ensuring the computer is secured after the script completes.

@ECHO OFF
TITLE Automated Task Runner

ECHO --- Starting a long-running process ---
ECHO This will take 10 seconds.
ECHO The workstation will be locked upon completion.
ECHO.
PAUSE
ECHO.

ECHO Running task...
REM (Simulate a long process)
TIMEOUT /T 10 /NOBREAK > NUL

ECHO.
ECHO --- Task complete. Locking workstation. ---

REM Give the user a moment to see the message before the screen locks.
TIMEOUT /T 3 > NUL

RUNDLL32.EXE user32.dll,LockWorkStation

Conclusion

The RUNDLL32.EXE user32.dll,LockWorkStation command is the definitive, simple, and most reliable method for locking a Windows workstation from a batch script.

Key takeaways:

  • The command is a single, easy-to-remember line.
  • It does not require administrator rights.
  • It is a non-destructive action that keeps the user's session active and their programs running.
  • Ensure there is no space between the .dll name and the function name.

By using this command, you can easily add security features to your scripts or create convenient shortcuts for common system actions.