I'm making a "shortcut" PS1 script to update a domain user's password. I'd like to prompt the user for the new password twice without showing the password on screen. When I use $Text1=Read-Host ; $Text2=Read-Host ; $Text1 -eq $Text2
with the same input - '1' for example - the output of that one-liner is "True". However,
$Text1=Read-Host -AsSecureString ; $Text2=Read-Host -AsSecureString ; $Text1 -eq $Text2
and
$Text1=Read-Host -AsSecureString ; $Text2=Read-Host -AsSecureString ; (ConvertFrom-SecureString $Text1) -eq (ConvertFrom-SecureString $Text2)
return False.
The script as it is now, without prompting twice and comparing user inputs, is below and it works to reset a user's password.
$UserName = Read-Host "User name "
$NewPass = Read-Host -AsSecureString
Set-ADAccountPassword `
-NewPassword $NewPass `
-Verbose `
-Identity ( (Get-ADUser -Filter "SamAccountName -like '$UserName'").DistinguishedName )
$NewPass.Dispose()