ninja-one/dontdisplaylastusername.ps1

#Requires -Version 5.1

<#
.SYNOPSIS
    This script attempts to leave the current domain and join a workgroup using CIM methods. If CIM fails, it falls back to using netdom.
    It also clears specific registry values related to the last logged-in user.
    Finally, it installs a provisioning package.
#>


begin {

}
process {
    try {
        $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"

        # Check and set dontdisplaylastusername to 0
        $dontDisplayLastUserName = Get-ItemProperty -Path $regPath -Name "dontdisplaylastusername" -ErrorAction SilentlyContinue

        if ($dontDisplayLastUserName.dontdisplaylastusername -ne 0) {
            Set-ItemProperty -Path $regPath -Name "dontdisplaylastusername" -Value 0 -Type DWord -Force
            Write-Host "[INFO] dontdisplaylastusername set to 0"
        }
        else {
            Write-Host "[INFO] dontdisplaylastusername is already set to 0"
        }
    
        Write-Host "[SUCCESS] Operation complete. The login screen will pre-select user at next restart."  -ForegroundColor Green
    }
    catch {
        Write-Host "[ERROR] Error clearing registry values: $_"  -ForegroundColor Red
        exit 1
        
    }

    Write-Host "[SUCCESS] Script execution completed."  -ForegroundColor Green

    exit 0
}
end {

}