网站搜索

Windows 10:使用 PowerShell 自动化配置自动登录


有时我们只需要一台具有所谓自动登录功能的计算机。此功能无疑存在安全风险,但另一方面,它是使计算机对所有用户而言尽可能简单的好方法。自动登录配置适用于 Kiosk 模式或演示计算机,其配置应尽可能简单。在这篇博文中,我将提供一个脚本来配置自动登录,而无需在注册表中搜索正确的密钥。让我们开始吧。

目标

在这一部分中,我将向您展示如果运行我的脚本会发生什么。

就是这样。很酷的东西。这让我想到了代码。

代码

在运行代码之前,请检查您的 PowerShell ExecutionPolicy 设置。在 Windows 10 计算机上,默认设置为“受限”。为了运行该脚本,您必须将策略至少更改为 RemoteSigned。

Get-ExecutionPolicy

Set-ExecutionPolicy RemoteSigned -Force

准备好 ?现在是聚会时间。

将以下代码复制到 PowerShell ISE 或您选择的编辑器中。运行代码并输入用户名和密码。然后重新启动计算机并检查其是否按预期工作。

### The code below configures Auto-Login on Windows computers ###

<#

Author: Patrick Gruenauer | Microsoft MVP on PowerShell
Web: https://sid-500.com

#>

$Username = Read-Host 'Enter username for auto-logon (f.e. contoso\user1)'
$Pass = Read-Host "Enter password for $Username"
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
Set-ItemProperty $RegistryPath 'AutoAdminLogon' -Value "1" -Type String 
Set-ItemProperty $RegistryPath 'DefaultUsername' -Value "$Username" -type String 
Set-ItemProperty $RegistryPath 'DefaultPassword' -Value "$Pass" -type String

Write-Warning "Auto-Login for $username configured. Please restart computer."

$restart = Read-Host 'Do you want to restart your computer now for testing auto-logon? (Y/N)'

If ($restart -eq 'Y') {

    Restart-Computer -Force

}


好啦,今天就到此为止。下次使用 PowerShell 再见。

警告:要在 Hyper-V 中测试此脚本,您必须为 Hyper-V 来宾禁用增强会话模式。

相关文章