SynopsisSets a user to automatically logon to a local or remote computer at start-up, with the option to lock the screen immediately.
- Set-AutoLogon [-Username] [-Password] [-Domain] [-LogonCount] [-AutoLock] [-ComputerName] [-LogonCredentials] [-Credential]
- Set-AutoLogon [-ComputerName] [-LogonCredentials] [-Credential]
DescriptionThe Set-Autologon cmdlet allows you to specify a user account to automatically logon to a computer at start up. The number of automated logons can be specified using the LogonCount parameter. If the LogonCount parameter is not specified the automatic logon will be made permanent. It should be noted that the password for the user is stored in the registry until the logon count expires or the Remove-AutoLogon cmdlet is run. Used in conjunction with the Get-PSLaunchLine and Set-StartUpApplication cmdlets, Set-Autologon can be used to launch Powershell scripts at boot time in specific user environments.
- Username The username to logon automatically. If no username is specified you will be prompted for credentials when the cmdlet is run.
- Password The password of the specified user. If no password is specified you will be prompted for credentials when the cmdlet is run.
- Domain The logon domain of the specified user.
- LogonCount The number of automated logons to make. Once this number is reached automatic logons stop and password information is removed from the registry. Specify a negative number to make automatic logon settings permanent.
- AutoLock Lock the screen immediately after logon. Used to secure the console after an automatic logon. This is useful, for example, when you require that the computer starts in a specific user environment when running scripts or installations, but that the console is not left exposed.
- ComputerName The computer against which to run the cmdlet. By default this parameter will be populated with the name of the local computer.
- Credential The credentials under which to run the cmdlet. By default this cmdlet will run as the current user. Using this parameter and the Get-PSCredential cmdlet you can specify an alternate set of credentials under which to execute this command.
InputsSet-AutoLogon accepts the output of the Get-AutoLogon cmdlet as an input object. In addition, the Username, Domain, LogonCount and ComputerName parameters each accept pipeline input by property name. The password parameter does not accept pipeline input.
OutputsSet-AutoLogon makes changes directly to the Windows registry and does not output any Powershell objects.
NotesThe Set-AutoLogon cmdlet requires that the RPC service is started on the computer it is trying to access and that DNS services are available to resolve target host names.
Example 1This command will set the local computer to automatically logon after the next reboot as the user 'myuser' on the domain 'mydomain.com'. After the user has automatically logged on once the logon count is reduced to zero and the password removed from the registry:
Set-AutoLogon -Username myuser -Password mypassword -Domain mydomain.com -logoncount 1
Example 2Identical to the first example, but without the user information, this command will prompt you for username and password information when run. If these are supplied the local computer will be set to automatically logon after the next reboot as the specifed user on the domain 'mydomain.com':
Set-AutoLogon -Domain mydomain.com -logoncount 1
Example 3Again identical to the first example, but this time with username and password data supplied via the $MyCreds variable in the LogonCredential parameter. In this example $MyCreds is a PSCredential object that has been created with the standard Powershell cmdlet Get-Credential.
$MyCreds = Get-CredentialSet-AutoLogon -Domain mydomain.com -logoncount 1 -LogonCredentials $MyCreds
Example 4This command will set the local computer to automatically logon after every reboot as the logoncount parameter has been set to a negative number. The Autolock parameter has also been set, forcing a screen lock immediately after the user has been logged on. This allows scripts, startup applications or installs to run in the context of the user without exposing the desktop. Both of the autolock and the automatic logon will remain active until this cmdlet is run with the -Clear parameter.
Set-AutoLogon -Username myuser -Password mypassword -Domain mydomain.com -logoncount -1 -Autolock
Example 5The Get-AutoLogon and Set-AutoLogon cmdlets can be used together, as in this example. This command will take the automatic logon settings of PC01 and replicate them on PC02. You will be prompted to re-enter the password information however, even if it is stored on PC01.
Get-AutoLogon PC01 | Set-Autologon PC02