Automatically login and lock

47,109

Solution 1

Take a look at this: Auto Logon and Lock

Auto Logon to Windows and immediately Lock Workstation (Safe Mode protected too)

Auto Logon & Lock is a small utility that enables your Windows PC to automatically logon to a user account on boot and then lock the desktop so a password is still required. Some of the reasons you might want to do this are: Faster boot time. The additional post-logon start up items are also started after Windows boots. Which means when you press the power button on your PC and come back after a few minutes, its completely booted up. No need to wait for additional software to load after you logon. Automatically resume downloads etc. when your PC reboots due to a power failure or crash. On XP you could do this by installing the application as a system service. In Vista and 7, Session 0 Isolation makes using a system service a pain, not to mention it's insecure. Just put your applications in the regular Windows Start Up folder and they will be launched after the auto logon. It password protects Safe Mode as well. Existing auto logon methods leave Safe Mode unprotected. It locks the desktop before Explorer is started. Other methods auto-lock using an autorun/start up entry which leave the PC logged on and unlocked until the autorun entries are executed (which can even take several minutes).

I haven't tried it myself, but it looks like it does what you want...

Solution 2

I managed to lock the PC immediately on startup while loading windows/startup items in the background. This works with Windows editions that has group policy editor.

Step 1: Open notepad, then paste this code:

WScript.CreateObject("WScript.Shell").Run("rundll32 user32.dll,LockWorkStation")

Step 2: Click File>Save As and in Save as type dropdown menu, choose All Files

Step 3: In the File Name field, enter LockWorkStation.vbs and save the file to C:\Users\YourUserName\Documents

Step 4: Hit WindowsKey+R, type regedit and press ENTER

Step 5: Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

Step 6: Right Click on a blank space and click New> DWORD (32-bit) Value and press ENTER

Step 7: Double click the newly created REG_DWORD file. In the Value name type RunLogonScriptSync and in the Value data type1 and then press ENTER

Step 8: Hit WindowsKey+R, type gpedit.msc and press ENTER

Step 9: Under Computer Configuration, go to Administrative Templates > System > Logon then Double Click Run these programs at user logon

Step 10: Click Enabled, and on Items to run at logon click Show...

Step 11: Type C:\Users\YourUserName\Documents\LockWorkStation.vbs and click OK repeatedly until all windows are closed

Step 12: Hit WindowsKey+R and type control.exe userpasswords2 then press ENTER

Step 13: Uncheck Users must enter a user name and password to use this computer. then click OK (type in your password if it prompts to do so)

Step : Restart your PC.

Now whenever you start your PC, the .vbs script will run first before anything else. This will ensure that your PC is locked before the desktop appears.

Note: You can change C:\Users\YourUserName\Documents\ to wherever you want to store your script.

Solution 3

You can implement this as a windows task scheduled to be performed at login:

Run program rundll32.exe With arguments user32.dll,LockWorkStation

It works on my XP and 7, however it won't work on Windows 8/8.1

Solution 4

I used to do this on my home computer (not any more). It involves just two simple steps.

  1. Set Windows to automatically log in to your account upon start-up (this will load all the start-up programs and take you to the desktop).
  2. Include a shortcut in your personal Startup folder (or the registry's Run key) to lock the computer, either wih the following command or with nircmd utility.

    rundll32.exe user32.dll,LockWorkStation
    

Since most start-up programs are loaded in parallel, your computer will be locked almost immediately upon being automatically logged into. There's minimum concern of someone snooping in and stopping the lock.

However, if you need to ensure the computer is locked as soon as possible, follow the order in this article (avoid RunOnce as this is deleted after run, unless you can place a counter-script somewhere else to add it back).
I found this KB article which lists the order proper for older versions of Windows, but I can't find an official equivalent for Windows 7.

Solution 5

This is old but since there are some concerns to use rundll32.exe

What’s the guidance on when to use rundll32? Easy: Don’t use it

Occasionally, a customer will ask, "What is Rundll32.exe and when should I use it instead of just writing a standalone exe?"

The guidance is very simple: Don't use rundll32. Just write your standalone exe.

Here is another approach which will save from compiling a standalone exe yourself. Just save this as Lock-Workstation.ps1 and run it with powershell.

Function Lock-WorkStation { 
#Requires -Version 2.0 
$signature = @" 
[DllImport("user32.dll", SetLastError = true)] 
public static extern bool LockWorkStation(); 
"@ 

$LockWorkStation = Add-Type -memberDefinition $signature -name "Win32LockWorkStation" -namespace Win32Functions -passthru 
$LockWorkStation::LockWorkStation() | Out-Null 
}

Lock-WorkStation

Taken from Script-Center

Share:
47,109

Related videos on Youtube

utapyngo
Author by

utapyngo

Updated on September 18, 2022

Comments

  • utapyngo
    utapyngo over 1 year

    I want to configure Windows 7 to login automatically after the computer is switched on. That's not because I'm lazy and don't like typing passwords. That's because I want programs that are configured to run on startup run automatically.

    But I don't want everyone to see my desktop, so I want my computer to remain locked. When I'm ready to work, I just type my password and don't have to wait until all startup programs run.

    • utapyngo
      utapyngo over 12 years
      Yes. I don't like hibernating because some programs lose their network connection and I have to restart them anyways.
    • Robert
      Robert over 12 years
      Which programs in detail? Have you tried to make the authors fix this problem?
    • surfasb
      surfasb over 12 years
      @eye:I don't get it. So because you have to restart these programs, you decide you need to restart your whole machine?? Am I missing something?
    • utapyngo
      utapyngo over 12 years
      @surfasb: one more reason is that it is faster to turn off and turn on my computer that dump 8 gigabytes of RAM and then read it back.
    • Dan Is Fiddling By Firelight
      Dan Is Fiddling By Firelight almost 12 years
      @surfasb another reason is that hibernating won't help a desktop that loses power unexpectedly. You can configure the BIOS to restart the computer when power is restored; but only services are restarted not any usermode applications.
    • surfasb
      surfasb almost 12 years
      @DanNeely: Very good point. Not all desktops are hooked up to a UPS.
  • utapyngo
    utapyngo over 12 years
    I know about this trick. However, if I put this batch file in startup, it would leave a security hole on my computer: until it gets running, someone bad could do someting bad. As I said, I don't want anyone to see my desktop.
  • Rory Alsop
    Rory Alsop over 12 years
    @eye - sadly, you are leaving a security hole by automatically logging in. This solution will at least work. I'd recommend not logging in automatically if you want security.
  • HaydnWVN
    HaydnWVN over 12 years
    Depending on your system it would run very quickly and lock... You could leave your monitor switched off too then the only indication your PC was on would be any lights on the front/keyboard?
  • utapyngo
    utapyngo over 12 years
    This looks more secure: instead of just putting itself to startup, it replaces the system shell (explorer) by itself.
  • Nate Koppenhaver
    Nate Koppenhaver over 12 years
    @eye this runs very quickly -- It would be hard for someone to gain access before it would lock because even if they started to open something it would lock while they were doing it
  • Dan Is Fiddling By Firelight
    Dan Is Fiddling By Firelight almost 12 years
    @utapyngo if it works by replacing explorer I'd strongly disagree on the security assessment. Explorer is a popular target for malware trying to break into a system; and I find it highly unlikely that a third party is able to put anywhere near as much effort into testing a replacement as MS is into testing the original.
  • utapyngo
    utapyngo almost 12 years
    @Dan: actually it does not replace explorer.exe but uses a feature of Windows which allows changing a user's shell by changing a registry value.
  • pioto
    pioto about 10 years
    What about unintentional shutdown? (e.g., a power outage, or some bad actor intentionally turning the system off and on again?)
  • srmark
    srmark over 9 years
    This works very well for me. Probably not 100% secure but good enough for my home use and non-invasive.
  • malix
    malix almost 8 years
    works on windows 10, save it to a .cmd and put it in shell:startup
  • ADTC
    ADTC almost 8 years
    There are ways to auto-login a user with a password. Besides, if you set a password on a passwordless user, you need to manually remove it for the passwordless login to work the next time. Plus the password is stored plain text in the script.
  • Evengard
    Evengard almost 7 years
    Thanks, that's the ACTUAL solution over here. Didn't knew about RunLogonScriptSync which is actually the key to solving this problem.
  • Tobias Kienzler
    Tobias Kienzler over 6 years
  • Limer
    Limer about 4 years
    You can replace steps 4 through 7 with storing the same information in a small .reg-file and importing that into the registry. That's easier and safer IMHO because you'd skip the manual editing of the registry. Take a look at this rejected suggested edit. - OT: I don't quite get why my edit was rejected - I don't see how it deviates from the original intent of the post or changes the goals of the post's owner. (And I couldn't find a proper way to ask for clarification.)
  • silico-biomancer
    silico-biomancer about 3 years
    I am on Windows home and substituted lusrmngr for gpedit, and this partially worked... except that the autologon seems to also auto-unlock the lock screen as soon as it appears
  • Community
    Community over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.