internal/functions/Get-RandomPassword.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function Get-RandomPassword {
    # generates a random secure password of a specified length
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
    Param (
        $Length = 15,
        [switch]$AsPlainText
    )
    $vocabulary = @{
        Group1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        Group2 = 'abcdefghijklmnopqrstuvwxyz'
        Group3 = '1234567890'
        Group4 = '!#$%&()*+,-./:;<=>?[\]^_{|}~'
    }
    $pwdPool = @()
    # get at least one of each symbols
    1..4 | Foreach-Object {
        $pwdPool += $vocabulary."Group$_".Substring((Get-Random -Minimum 0 -Maximum $vocabulary["Group$_"].Length), 1)
    }
    # now get remaining random symbols
    if ($Length -gt 4) {
        5..$Length | Foreach-Object {
            $group = Get-Random -Minimum 1 -Maximum 5
            $pwdPool += $vocabulary."Group$group".Substring((Get-Random -Minimum 0 -Maximum $vocabulary["Group$group"].Length), 1)
        }
    }
    $password = (($pwdPool | Sort-Object { Get-Random }) -join '').Substring(0, $Length)
    if ($AsPlainText) { return $password }
    else { ConvertTo-SecureString $password -AsPlainText -Force }
}