I created a PS script to Restart and autologin Windows after execution
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$DefaultUsername = "Username-Example"
$DefaultPassword = "Password-Example"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -type String
Set-ItemProperty $RegPath "DefaultUsername" -Value "$DefaultUsername" -type String
Set-ItemProperty $RegPath "DefaultPassword" -Value "$DefaultPassword" -type String
Set-ItemProperty $RegPath "AutoLogonCount" -Value "1" -type DWord
Start-Sleep -Seconds 15 ; Restart-Computer -Force
Is there a command i could integrate in this script to hide/encrypt the plain text password and still be readable/usable by the script/windows?
My PS coding skill is limited,so any help is appreciated
>Solution :
To securely store and retrieve credentials in a PowerShell script, you can use the Get-Credential cmdlet to prompt the user for their credentials and then store them securely using the Windows Credential Manager. This approach avoids storing plaintext passwords in your script. Here’s how you can modify your script:
# Prompt the user for their credentials and store them securely
$credential = Get-Credential -Message "Enter your credentials"
# Store the credentials in Windows Credential Manager
$credential | New-StoredCredential -Target "MyTargetName"
# Set up autologon with the stored credentials
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -Type String
Set-ItemProperty $RegPath "DefaultUsername" -Value "$($credential.UserName)" -Type String
Set-ItemProperty $RegPath "AutoLogonCount" -Value "1" -Type DWord
# Restart the computer
Start-Sleep -Seconds 15
Restart-Computer -Force