≡ Menu

Powershell script to enable auto admin login on windows 7 computers

I came across a useful powershell function which allows you to set auto admin login on local/remote computer easily. Thought of sharing it with my blog readers.

You have few pre-requisites to make this function work against remote computer.

1. Remote Registry should running

2. You should have proper access to WMI name space

In easy words, running with domain admin privileges works great.

Function Enable-AutoAdminLogon {
param (
  [Parameter(Mandatory=$false)]
     [String[]]$computerName = “.”,
  [Parameter(Mandatory=$false)]
  [String]$DefaultDomainName = $env:USERDOMAIN,
  [Parameter(Mandatory=$false)]
  [String]$DefaultUserName = $env:USERNAME,
  [Parameter(Mandatory=$true)]
  [String]$DefaultPassword,
  [Parameter(Mandatory=$false)]
  [Int]$AutoLogonCount
)
if ([IntPtr]::Size -eq 8) {
  $hostArchitecture = “amd64”
} else {
  $hostArchitecture = “x86”
}
foreach ($computer in $computerName) {
  if (($hostArchitecture -eq “x86”) -and ((Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem).OSArchitecture -eq “64-bit”)) {
   Write-Host “Remote System’s OS architecture is amd64. You must run this script from x64 PowerShell Host”
   continue
  } else {
   if ($computer -ne “.”) {
    if ((Get-Service -ComputerName $computer -Name RemoteRegistry).Status -ne “Running”) {
     Write-Error “remote registry service is not running on $($computer)”
     continue
    } else {
     Write-Verbose “Adding required registry values on $($computer)”
     $remoteRegBaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’,$computer)
     $remoteRegSubKey = $remoteRegBaseKey.OpenSubKey(“SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon”,$true)
     $remoteRegSubKey.SetValue(“AutoAdminLogon”,1,[Microsoft.Win32.RegistryValueKind]::String)
     $remoteRegSubKey.SetValue(“DefaultDomainName”,$DefaultDomainName,[Microsoft.Win32.RegistryValueKind]::String)
     $remoteRegSubKey.SetValue(“DefaultUserName”,$DefaultUserName,[Microsoft.Win32.RegistryValueKind]::String)
     $remoteRegSubKey.SetValue(“DefaultPassword”,$DefaultPassword,[Microsoft.Win32.RegistryValueKind]::String)
     if ($AutoLogonCount) {
      $remoteRegSubKey.SetValue(“AutoLogonCount”,$AutoLogonCount,[Microsoft.Win32.RegistryValueKind]::DWord)
     }
    }
   } else {
    #do local modifications here
    Write-Verbose “Adding required registry values on $($computer)”
    Write-Verbose “Saving curent location”
    Push-Location
    Set-Location “HKLM:SoftwareMicrosoftWindows NTCurrentversionWinLogon”
    New-ItemProperty -Path $pwd.Path -Name “AutoAdminLogon” -Value 1 -PropertyType “String” -Force | Out-Null
    New-ItemProperty -Path $pwd.Path -Name “DefaultUserName” -Value $DefaultUserName -PropertyType “String” -Force | Out-Null
    New-ItemProperty -Path $pwd.Path -Name “DefaultPassword” -Value $DefaultPassword -PropertyType “String” -Force | Out-Null
    New-ItemProperty -Path $pwd.Path -Name “DefaultDomainName” -Value $DefaultDomainName -PropertyType “String” -Force | Out-Null
    if ($AutoLogonCount) {
     New-ItemProperty -Path $pwd.Path -Name “AutoLogonCount” -Value $AutoLogonCount -PropertyType “Dword” -Force | Out-Null
    }
    Write-Verbose “restoring earlier location”
    Pop-Location
   }
  }
}
}

[Source : Ravichaganti.com]

Comments on this entry are closed.

  • hans January 13, 2011, 9:23 pm

    If you don’t want to keep your user credentials in registry in plan text but need scripting, the Logonexpert could be useful for you, it has powerful enough command line interface for scripting (poweshell, vbscript, active perl, .bat files etc), scheduling (Windows scheduler) or remote execution via Psexec (comes with PsTools)

  • Sitaram Pamarthi January 15, 2011, 2:59 am

    Will you be able to provide a example to understand how this tool(LE) can be used to store password in a secure manner and interact with scripts too?