SetAutoLogon.ps1

<#PSScriptInfo
 
.VERSION 1.2
 
.GUID 815e6277-8e79-439f-8a07-34e4e1c5c682
.Author SAGSA
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
.NOTES
    Author: SAGSA
    https://github.com/SAGSA
    Requires: Powershell 2.0
#>


<#
 
.DESCRIPTION
 Set Autologon
 
#>
 
try
{
    $TestAdmin = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $IsAdmin=$TestAdmin.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    function ButtonOk([string]$LabelText="Success")
    {
        $button_ok = New-Object -TypeName System.Windows.Forms.Button
        $button_ok.Size = New-Object -TypeName System.Drawing.Size(75,23)
        $button_ok.Location = New-Object -TypeName System.Drawing.Size(60,20)
        $button_ok.Text = 'OK'
        $button_ok.Add_Click({$Result=$button_ok.Text;$form.Close()})
    
        $form=New-Object System.Windows.Forms.Form
        $form.Size = New-Object System.Drawing.Size(200,100)
        $form.StartPosition='CenterScreen'
        $form.MinimizeBox=$false
        $form.MaximizeBox=$false
        $form.Text=$LabelText
        $form.Controls.Add($button_ok)
        $form.AcceptButton = $button_ok
        $form.ShowDialog()
    }
    function CheckCredential
    {
        [cmdletbinding()]
        param(
            [parameter(Mandatory=$true)]
            $Credential
        )
        $ArgumentList=@(
            "-NoProfile"
            "-WindowStyle Hidden"
            "-Command Get-Host"
        )
        try
        {
            Start-Process "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe" -ArgumentList $ArgumentList -Credential $Credential -ErrorAction Stop
            $true
        }catch{
            $false
        }
    

    }
    if ($IsAdmin)
    {
        try
        {
            #$LoggedInUser=(Get-WmiObject -Class Win32_ComputerSystem).UserName
            do
            {
                Write-Verbose "Enter the username and password of the user under which auto-login will be performed.. Empty passwords not allowed!" -Verbose
                $Repeat=$true
                if ($Cred -eq $null)
                {
                    $Cred=Get-Credential "$Env:Userdomain\$env:USERNAME" -ErrorAction Stop
                    $DefaultUserName=$Cred.UserName
                }
                else
                {
                    $DefaultUserName=$Cred.UserName
                    if ($DefaultUserName -match "^\\")
                    {
                        $DefaultUserName=$DefaultUserName -replace "^\\",""
                    }
                    $Cred=Get-Credential $DefaultUserName -ErrorAction Stop
                }
            
                if (!(CheckCredential -Credential $Cred))
                {
                    Write-Error "Incorrect Credential!!! Check Username or Password, perhaps the user $($Cred.UserName) does not exist or disabled!" -ErrorAction Continue
                }
                else
                {
                    $Repeat=$False
                }
            }while($Repeat)
        
            if ($Cred -eq $null)
            {
                Write-Error "Credentiall is null" -ErrorAction Stop
            }
    
            function GetPlainTextPassword ($SecString)
            {
                $BSTR =[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecString)
                $PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
                $PlainPassword
            }

            $DefaultUserName=$Cred.UserName
            [string]$DefaultPassword=GetPlainTextPassword -SecString $Cred.Password
            if ($DefaultUserName -match "^\\")
            {
                $DefaultUserName=$DefaultUserName -replace "^\\",""
            }
            elseif(!($DefaultUserName -match ".+\\.+"))
            {
                $DefaultUserName=$env:COMPUTERNAME+"\"+$DefaultUserName
            }
        

            Write-Verbose "DefaultUsername $DefaultUserName DefaultPassword $DefaultPassword" -Verbose
            Write-Verbose "Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name DefaultUsername -Value $DefaultUserName" -Verbose
            Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name DefaultUsername -Value $DefaultUserName -ErrorAction Stop
            Write-Verbose "Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name DefaultPassword -Value $DefaultPassword" -Verbose
            Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name DefaultPassword -Value $DefaultPassword -ErrorAction Stop
            Write-Verbose "Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name AutoAdminLogon -Value 1" -Verbose 
            Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\' -Name AutoAdminLogon -Value 1 -ErrorAction Stop
            Write-Verbose "AutoLogon Success!!! User $DefaultUserName Password $DefaultPassword Restart computer.." -Verbose
            Start-Sleep -Seconds 1
            ButtonOk
        }
        catch
        {
            Write-Verbose "Error!!!!!" -Verbose
            Write-Error $_
            ButtonOk -LabelText "Error!!!!!!!!"
        }

    
    
    }
    else
    {
        Write-Verbose "Administrator require..Running This Script As Administrator" -Verbose
        $ArgumentList=@(
            "-NoProfile"
            #"-WindowStyle Hidden"
            '-File '+'"'+$($MyInvocation.MyCommand.Definition)+'"'
        )
        Write-Verbose "Start-Process -FilePath $env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList $ArgumentList -Verb runas"
        Start-Process -FilePath $env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList $ArgumentList -Verb runas -ErrorAction Stop
    }


    #$PsScriptRoot,$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    #$($MyInvocation.MyCommand.Source)
}
catch
{
    Write-Error $_
    Start-Sleep -Seconds 10
}