User creation with password and autologin after reboot
Here is the script I wrote:
$MyPassword = ConvertTo-SecureString (-join([char[]](33..122) | Get-Random -Count 10)) -AsPlainText -Force
new-localuser -name "Candidat" -Description "Compte Candidat" -password $MyPassword -UserMayNotChangePassword -AccountNeverExpires
Add-LocalGroupMember -Group 'Utilisateurs' -Member ('Candidat') –Verbose
# Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PasswordLess\Device" -Name "DevicePasswordLessBuildVersion" -Value 0
$MyUsername = 'Candidat'
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoAdminLogon" -Value 1
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUsername" -Value $MyUsername -type String
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value $MyPassword -type String
Restart-Computer -Force
But It doesn't work, the session opening is not authorized.
English is a second language for me, sorry for mistakes
Answers (2)
Top Answer
It is not working because your Default Password is being set to "System.Security.SecureString" exactly as the text, not the secure password you are trying to randomly create. Change your first line to these two lines:
$GetRandom = -join([char[]](33..122) | Get-Random -Count 10)
$MyPassword = ConvertTo-SecureString ($GetRandom) -AsPlainText -Force
And change the line for setting the default password in the registry to:
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value $GetRandom -type String
I will assume you already have a local security group called UtilisateursIt just works, thanks a lot, it's been a week for me to idle on this script
Fred
here is the corrected script:
# Génération du mot de passe$GetRandom = -join([char[]](33..122) | Get-Random -Count 10)$MyPassword = ConvertTo-SecureString ($GetRandom) -AsPlainText -Force# Création utilisateurnew-localuser -name "Candidat" -Description "Compte Candidat" -password $MyPassword -UserMayNotChangePassword -AccountNeverExpiresAdd-LocalGroupMember -Group 'Utilisateurs' -Member ('Candidat') –Verbose$MyUsername = 'Candidat'# Activation AutologonSet-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "AutoAdminLogon" -Value 1Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUsername" -Value $MyUsername -type String Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultPassword" -Value $GetRandom -type String Restart-Computer -Force