Verify Passwords Match in Windows Powershell
<p>I'm creating a script to handle unattended domain joining for the school district I work at.  We have several IT guys who handle sysprep, so I'm creating a script that will encrypt passwords to use for Add-Computer. </p>

<p>What I am having trouble with is having a script that takes two password entries, and restarts if they don't match, but continues if they do.  What I've tried so far:</p>

<pre><code>$s = {write-host "running script}
&amp;$s
$pwd1 = Read-Host -AsSecureString "Enter Password"
$pwd2 = Read-Host -AsSecureString "Enter Again"
If($pwd1 -ceq $pwd2) {
Write-host "match"
} else {
&amp;$s
}
</code></pre>

<p>I would like to have the script automatically make the user retry until both passwords match.</p>

<p>EDIT: Figured it out!  Here's the code for reference. Thanks to RowdyVinson!</p>

<pre><code>do {
Write-Host "I am here to compare the password you are entering..."
$pwd1 = Read-Host "Password" -AsSecureString
$pwd2 = Read-Host "Re-enter Password" -AsSecureString
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd1))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd2))
}
while ($pwd1_text -ne $pwd2_text)
Write-Host "Passwords matched"
</code></pre>