Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Set-AutLogon.ps1
- # This script does the following:
- # - Force logoff all users.
- # - Edits AutoLogon registry keys with specified input.
- # - Restarts the computer.
- #
- # The supplied password is stored in plain text within the Registry; however,
- # this value is delete once the specified number of logons has occurred.
- #
- # Tested with PowerShell 4.0 on Windows 7 Enterprise.
- Param(
- [parameter(Mandatory=$true)]
- [String[]]
- $ComputerID,
- [parameter(Mandatory=$true)]
- [String]
- $Username,
- [parameter(Mandatory=$true)]
- [String]
- $Password,
- [parameter(Mandatory=$false)]
- [Int]
- $TimesToLogin = 1
- )
- $credentials = Get-Credential
- ForEach ($computer in $ComputerID) {
- Invoke-Command -ComputerName $computer -Credential $credentials -ScriptBlock {
- Param($Times, $User, $Pass)
- $shutdown = $env:SystemRoot + "\System32\shutdown.exe"
- Start-Process -FilePath $shutdown -ArgumentList '-l -f' -Wait
- $path = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
- Set-ItemProperty -Path $path -Name AutoAdminLogon -Value 1
- Set-ItemProperty -Path $path -Name DefaultUserName -Value $User
- try {
- if (-Not (Get-ItemProperty -Path $path | Select-Object -ExpandProperty "DefaultPassword" -ErrorAction SilentlyContinue )) {
- New-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
- }
- } catch {
- New-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
- }
- Set-ItemProperty -Path $path -Name DefaultPassword -Value $Pass
- try {
- if (-Not (Get-ItemProperty -Path $path | Select-Object -ExpandProperty "AutoLogonCount" -ErrorAction SilentlyContinue )) {
- New-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
- }
- } catch {
- New-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
- }
- Set-ItemProperty -Path $path -Name AutoLogonCount -Value $Times
- Restart-Computer -Force
- } -ArgumentList $TimesToLogin,$Username,$Password
- }
- # Examples:
- # Specify a single Hostname or IP address. This example will AutoLogon 1 time with the specified account.
- # ./Set-AutoLogon.ps1 -ComputerID 10.20.20.105 -Username "Domain\Username" -Password "myPassword" -TimesToLogin 1
- #
- # Specify a range of either Hostnames or IP address by importing from a CSV file.
- # The '.Hosts' listed below references the column heading in the CSV file.
- # ./Set-AutoLogon.ps1 -ComputerID $( (Import-Csv -Path "C:\hosts.csv").Hosts ) -Username "Domain\Username" -Password "myPassword"
Add Comment
Please, Sign In to add comment