PoshCode Logo PowerShell Code Repository

Password Generator by AlphaSun 12 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6295"></script>download | new post

Functions: New-PIN, New-Password, New-PassPhrase

Provides quick access to randomly generated PINs, Passwords, and Pass Phrases.

New-PIN: Provides PINs of various lengths from 1-19 digits as Integers and PINs of 20 or more digits as Strings.

New-Password: Provides a random password with the given parameters. The default length is 12 characters. If you wish to omit a particular character set (eg. Symbols), you need to specify the total password length as well as how many characters from each set should be included. The sum of all selected sets should equal the total number of characters desired in the password. If the sum of the selected sets is less than the total, it is possible that a character from the undesired set may be included in the generated password.

New-PassPhrase: Provides a random pass phrase using words from a word list loaded into the $PasswdList variable. The overall security of the provided passwords depends greatly on the size of the word list you use—the more words, the bigger the haystack. With no defined switches, the function will randomly select 4 words from the word list, generate 4 random digits, and use the “-” as a separator character. Words are randomly capitalized. The number of words, number of digits, and separator character are configurable in addition to your choice of word list to use. A word list of 5,000-20,000 words is recommended.

Check-Even: Simple function to check if a number is even. Used in the New-PassPhrase function to randomize capitalization of words.

  1. # Wordlist of choice. Used with New-PassPhrase function.
  2. $PasswdList = Import-CSV $ENV:UserProfile\words.csv
  3.  
  4. function Check-Even($num){[bool]!($num%2)}
  5.  
  6. function New-PIN{
  7.         [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  8.         param(
  9.                 [parameter(Mandatory = $true, Position = 0)]
  10.                 [ValidateRange(1,([int]::MaxValue))][int]$Digits = 1
  11.         )
  12.         $NC = 0
  13.         [string]$PIN = ""
  14.         While($NC -lt $Digits){
  15.                 $PIN += Get-Random -Minimum 0 -Maximum 10
  16.                 $NC += 1
  17.         }
  18.         If($Digits -lt 19){
  19.                 return [int64]$PIN}
  20.         Else{return [string]$PIN}
  21. }
  22.  
  23. function New-Password{
  24.     [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  25.     param(
  26.         [parameter(Mandatory = $true, Position = 0)]
  27.         [ValidateRange(1,([int]::MaxValue))][int]$Total = 12,
  28.         [ValidateRange(0,([int]::MaxValue))][int]$Upper = 0,
  29.         [ValidateRange(0,([int]::MaxValue))][int]$Lower = 0,
  30.         [ValidateRange(0,([int]::MaxValue))][int]$Numeric = 0,
  31.         [ValidateRange(0,([int]::MaxValue))][int]$Symbol = 0,
  32.     )
  33.     Process{
  34.     if(($Upper+$Lower+$Numeric) -gt $Total){
  35.         throw "New-Password : Cannot validate argument on parameter 'Total'. The $Total argument is less than the sum of parameters 'Upper', 'Lower', 'Numeric', and 'Symbol'.
  36.    Supply an argument that is greater than or equal to the sum of parameters 'Upper', 'Lower', 'Numeric', and 'Symbol'."
  37.     }
  38.         $UC = (65..90)
  39.         $LC = (97..122)
  40.         $NU = (48..57)
  41.         $SY = (33..38 + 40..47 + 58..64 + 91..95 + 123..126)
  42.     [int[]]$IArr = New-Object System.Int32
  43.     If($Upper -ge 1){
  44.         $IArr += Get-Random -Input $($UC) -Count $Upper
  45.     }
  46.     If($Lower -ge 1){
  47.         $IArr += Get-Random -Input $($LC) -Count $Lower
  48.     }
  49.     If($Numeric -ge 1){
  50.         $IArr += Get-Random -Input $($NU) -Count $Numeric
  51.     }
  52.     If($Symbol -ge 1){
  53.         $IArr += Get-Random -Input $($SY) -Count $Symbol
  54.     }
  55.     If($Total -gt ($Upper+$Lower+$Numeric+$Symbol)){
  56.         $IArr += Get-Random -Input $($UC + $LC + $NU + $SY) -Count ($Total - $Upper - $Lower - $Numeric - $Symbol)
  57.     }
  58.     $IArr =  $IArr -ne 0
  59.     return ([char[]](Get-Random -InputObject $IArr -Count $IArr.Count)) -join ""
  60.     }
  61. }
  62.  
  63. function New-PassPhrase{
  64. [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  65. param(
  66.         [parameter(Mandatory = $false, Position = 0)]
  67.         [ValidateRange(1,([int]::MaxValue))][int]$Words = 4,
  68.         [parameter(Mandatory = $false, Position = 1)]
  69.         [ValidateRange(0,([int]::MaxValue))][int]$Numbers = 4,
  70.         [ValidateSet("~","!","@","#","$","%","^","&","*","-","_","=","+",";",":",",",".","|"," ")][string]$Separator = "-"
  71. )
  72.  
  73.         $WC = 0
  74.         While($WC -lt $Words){
  75.         $Case = (Get-Random -Minimum 0 -Maximum 100)
  76.         If((Check-Even $Case) -eq $True){
  77.                 $Word += ($(Get-Random -Input $PasswdList).Word).ToUpper()
  78.                 $Word += $Separator
  79.                 $WC += 1
  80.         }
  81.         elseIf((Check-Even $Case) -eq $False){
  82.                 $Word += ($(Get-Random -Input $PasswdList).Word).ToLower()
  83.                 $Word += $Separator
  84.                 $WC += 1
  85.         }
  86.         }
  87.  
  88.         $NC = 0
  89.         If($Words -eq 0){[string]$Word = ""}
  90.         While($NC -lt $Numbers){
  91.                 $Word += Get-Random -Minimum 0 -Maximum 10
  92.                 $NC += 1
  93.         }
  94.         return [string]$Word
  95. }

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