How can I lock a workstation from the command line?

John Savill

May 27, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Windows 2000 enables you to lock the workstation by typing thefollowing command:

C:> rundll32.exe user32.dll,LockWorkStation

You could also use this as a shortcut on you desktop.

This will not work in Windows NT 4.0. To achieve a similar effect requires abit more work:

You can use the SendMessage call to send a SC_SCREENSAVE message to the topmost window, which locks the workstation if the current user has a screen saver configured and the screen saver is configured to require a password.

Related: "10 Steps to Lock Down Desktops" 

To ensure that a screen saver is configured, you can create registry entries. Run a registry (.reg) file that adds the following entries from the command line: 

HKCUControl PanelDesktopScreenSaveActive = 1
HKCUControl PanelDesktopScreenSaverIsSecure = 1
HKCUControl PanelDesktopScreenSaveTimeout = timeout in seconds
HKCUControl PanelDesktopSCRNSAVE.EXE = %SystemRoot%System32Appropriate screen saver.scr
Some screen savers require additional parameters in HKCUControl PanelScreen Saver.screen saver name as well.

After you create these registry entries, the following call invokes the screen saver: 

SendMessage(HWND_TOPMOST, WM_SYSCOMMAND, SC_SCREENSAVE, 0) 

You must write and compile a short program (.exe file) that contains this call. You can then call your program from the command line to activate the screen saver. Because the ScreenSaverIsSecure value in the registry has been set to 1, this has the effect of locking the workstation.

Related: "More on Locking down that desktop"

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like