user1846439
user1846439

Reputation: 387

Test password has special character in powershell script

I am trying to build a script that checks that the password has letters and numbers and special characters. This is what I have so far:

 Write-Output " Enter db Password - DO NOT Use spaces - " 
 Write-Output " No part of the username can be contained in the password"

  do {$pdb=read-host " Must have 8 characters include at least 1 number & 1 special character i.e. help@menow1"

   $string = $pdb
   $pat = "^[a-zA-Z0-9\s]+$"
   $special = [regex]"[^a-zA-Z0-9]"
   }

  while (!$pdb -or 

      !($pdb.length -ge 8) -or

      ($pdb.Contains($pat)) -or 

      ($pdb.Contains($special))

      ) 

It does most of what I want. If I input as the password cat and press enter it prompts again for the password to be entered. If I put in cats1234, this passes and the script ends. What I want for it to prompt again and only pass if I put in cats1234$.

Thanks!

Upvotes: 0

Views: 2085

Answers (1)

Shay Levy
Shay Levy

Reputation: 126842

Give this a try:

do {
    $psw=read-host "Must have 8 characters include at least 1 number & 1 special character i.e. help@menow1"
} while (
    $psw.length -lt 8 -or $psw -notmatch '[^a-zA-Z0-9]' -or $psw -notmatch '\d'
) 

$psw

Upvotes: 1

Related Questions