PoshCode Logo PowerShell Code Repository

New-ComplexPassword by Jon Webster 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/567"></script>download | new post

Generate random passwords with specific complexity requirements

  1. # $Id: New-ComplexPassword.ps1 170 2008-09-05 19:49:48Z jon $
  2. # $Revision: 170 $
  3.  
  4. Function New-ComplexPassword ([int]$Length=8, $digits=$null, $alphaUpper=$null, $alphaLower=$null, $special=$null)
  5. {
  6.  
  7.         #  ASCII data taken from http://msdn2.microsoft.com/en-us/library/60ecse8t(VS.80).aspx
  8.  
  9.         # Make sure the password is long enough to meet complexity requirements
  10.         if($digits+$alphaUpper+$alphaLower+$special -gt $Length) { throw "Password too short for specified complexity" }
  11.  
  12.         # Define character groups and the number of each required by passwords
  13.  
  14.         # In case this is used in a DCPromo answer files, theres a few chars to
  15.         # avoid: Ampersand, Less than, double quote and back slash
  16.         # (34,38,60,92)
  17.        
  18.         $groups = @()
  19.         $group = New-Object System.Object
  20.         Add-Member -In $group -Type NoteProperty -Name "Group" -Value "0123456789" # 48..57
  21.  
  22.         Add-Member -In $group -Type NoteProperty -Name "Count" -Value $Digits
  23.         $groups += $group
  24.  
  25.         $group = New-Object System.Object
  26.         Add-Member -In $group -Type NoteProperty -Name "Group" -Value "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # 65..90
  27.         Add-Member -In $group -Type NoteProperty -Name "Count" -Value $alphaUpper
  28.         $groups += $group
  29.  
  30.         $group = New-Object System.Object
  31.         Add-Member -In $group -Type NoteProperty -Name "Group" -Value "abcdefghijklmnopqrstuvwxyk" # 97..122
  32.         Add-Member -In $group -Type NoteProperty -Name "Count" -Value $alphaLower
  33.         $groups += $group
  34.  
  35.         $group = New-Object System.Object
  36.         Add-Member -In $group -Type NoteProperty -Name "Group" -Value '~`!@#$%^&*()-_={}[]\|;:"<>?,./'' ' #  32..47, 58..64, 91..96, 123..126
  37.         Add-Member -In $group -Type NoteProperty -Name "Count" -Value $special
  38.         $groups += $group
  39.  
  40.         # initilize random number generator
  41.         $ran = New-Object Random
  42.  
  43.         # make sure password meets complexity requirements
  44.         foreach ($req in $groups)
  45.         {
  46.                 if ($req.count)
  47.                 {
  48.                         $charsAllowed += $req.group
  49.  
  50.                         for ($i=0; $i -lt $req.count; $i++)
  51.                         {
  52.                                 $r = $ran.Next(0,$req.group.length)
  53.                                 $password += $req.group[$r]    
  54.                         }
  55.                 } elseif ($req.count -eq 0) {
  56.                         $charsAllowed += $req.group
  57.                 }
  58.         }
  59.  
  60.         # make sure password meets length requirement
  61.         if(!$charsAllowed)
  62.         {
  63.                 $groups |% { $charsAllowed += $_.group }
  64.         }
  65.  
  66.         for($i=$password.length; $i -lt $length; $i++)
  67.         {
  68.                 $r = $ran.Next(0,$charsAllowed.length)
  69.                 $password += $charsAllowed[$r]
  70.         }
  71.  
  72.         # randomize the password
  73.         return [string]::join('',($password.ToCharArray()|sort {$ran.next()}))
  74. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me