0

I am trying to generate a 6 character password but I want each letter to match correct RegEx.

The RegEx being:

[a-z][A-Z][0-9][a-z][!@#$%^&*?+][A-Z]

I am currently here:

$passwd = @(1..6)
For ($i = 0; $i -lt $passwd.length; $i++){
Get-Random $passwd[$i] 
}

But all this is doing is generating 6 random integers.

I want to use the Get-Random cmdlet

Musa
  • 553
  • 1
  • 7
  • 19

2 Answers2

1

It's "generating 6 random integers" because that's exactly what you're telling it to do. $passwd is an array of integers 1 through 6, and then you're basically calling Get-Random 6 times by looping through that array:

Get-Random 1
Get-Random 2
Get-Random 3
Get-Random 4
Get-Random 5
Get-Random 6

Instead, you need arrays of each of your allowed/required characters, then select from those, and put them together. See this answer for a solution to a very similar situation.

Community
  • 1
  • 1
alroc
  • 27,574
  • 6
  • 51
  • 97
0

OK.

@(
  [char](97..122 | get-random),
  [char](65..90 | get-random),
  (0..9 | get-random),
  [char](97..122 | get-random),
  ('!','@','#','$','%','^','&','*','?',';','+' | get-random),
  [char](65..90 | get-random)
 ) -join ''
mjolinor
  • 66,130
  • 7
  • 114
  • 135