Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell script (hide password)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading