Powershell – Get-RandomPassword

Get a random password function in powershell, it gets a random password of specified length containing numbers, uppercase letters, # lowercase letters and special characters. Minimum length required is 4 characters # since password must contain all four types of characters.

Get-RandomPassword (20)

returns a 20 character long password

#Requires -RunAsAdministrator 
#Requires -Modules ActiveDirectory

function Get-RandomPassword () 
    # Gets a random password of specified length containing numbers, uppercase letters,
    # lowercase letters and special characters. Minimum length required is 4 characters
    # since password must contain all four types of characters.
    {
    Param( [int]$length)
    $Password = $Null
    if ($length -lt 4) {Write-Error -message " 4 is the minimum password length";return $Password}
    
    # Define list of numbers, this will be CharType 1
    $numbers=$null
    For ($a=48;$a –le 57;$a++) {$numbers+=,[char][byte]$a }
 
    # Define list of uppercase letters, this will be CharType 2
    $uppercase=$null
    For ($a=65;$a –le 90;$a++) {$uppercase+=,[char][byte]$a }

    # Define list of lowercase letters, this will be CharType 3
    $lowercase=$null
    For ($a=97;$a –le 122;$a++) {$lowercase+=,[char][byte]$a }

    # Define list of special characters, this will be CharType 4
    $specialchars=$null
    For ($a=33;$a –le 47;$a++) {$specialchars+=,[char][byte]$a }
    For ($a=58;$a –le 64;$a++) {$specialchars+=,[char][byte]$a }
    For ($a=123;$a –le 126;$a++) {$specialchars+=,[char][byte]$a }
    # Need to ensure that result contains at least one of each CharType
    # Initialize buffer for each character in the password
    $Buffer = @()
    For ($a=1;$a –le $length;$a++) {$Buffer+=0 }

    # Randomly chose one character to be number
    while ($true)
    {
        $CharNum = (Get-Random -minimum 0 -maximum $length)
        if ($Buffer[$CharNum] -eq 0) {$Buffer[$CharNum] = 1; break}
    }
    # Randomly chose one character to be uppercase
    while ($true)
    {
        $CharNum = (Get-Random -minimum 0 -maximum $length)
        if ($Buffer[$CharNum] -eq 0) {$Buffer[$CharNum] = 2; break}
    }
    # Randomly chose one character to be lowercase
    while ($true)
    {
        $CharNum = (Get-Random -minimum 0 -maximum $length)
        if ($Buffer[$CharNum] -eq 0) {$Buffer[$CharNum] = 3; break}
    }
    # Randomly chose one character to be special
    while ($true)
    {
        $CharNum = (Get-Random -minimum 0 -maximum $length)
        if ($Buffer[$CharNum] -eq 0) {$Buffer[$CharNum] = 4; break}
    }
    # Cycle through buffer to get a random character from the available types
    # if the buffer already contains the CharType then use that type
    $Password = ""
    foreach ($CharType in $Buffer) {
    if ($CharType -eq 0) {$CharType = ((1,2,3,4)|Get-Random)}
    switch ($CharType)
        {
        1 {$Password+=($numbers | Get-Random)}
        2 {$Password+=($uppercase | Get-Random)}
        3 {$Password+=($lowercase | Get-Random)}
        4 {$Password+=($specialchars | Get-Random)}
        }
    }
    return $Password
}

Get-RandomPassword (20)

returns a 20 character long password

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.