How to use Read-Host cmdlet with AsSecureString parameter?

by Dec 15, 2012

After the user enters a password, the only value available to the scripter is the password length.

It's not even possible to compare 2 SecureString objects.

In the script below, the 2 SecureString objects will always be different, according to the 3 checks made, even if the user enters 2 identical passwords:

 

$equal={Write-Host -ForegroundColor Green 'equal!'}
$notequal={Write-Host -ForegroundColor Red 'NOT equal!'}
$checking={Write-Host -ForegroundColor Cyan 'Checking…'}

$pwd1 = Read-Host 'Enter a Password 1' -AsSecureString
"`$pwd1=$pwd1"
"`$pwd1 Length=$($pwd1.Length)`n"

$pwd2 = Read-Host 'Enter a Password 2' -AsSecureString
"`$pwd2=$pwd2"
"`$pwd2 Length=$($pwd2.Length)`n"

& $checking
if ($pwd1 -eq $pwd2){& $equal}else{& $notequal}

& $checking
if ($pwd1.Equals($pwd2)){& $equal}else{& $notequal}

& $checking
if ([System.Object]::Equals($pwd1,$pwd2)){& $equal}else{& $notequal}

 

How can I compare 2 SecureString objects?