Public/New-Password.ps1

function New-Password {
    <#
        .SYNOPSIS
        Creates a random passphrase or password.

        .DESCRIPTION
        Creates a passphrase or password based on a large english word dictionary.
        It will fall back to pure random generation if dictionary is unreachable.

        .PARAMATER Length
        Specifies the minimum length for the password.

        .INPUTS
        None.

        .OUTPUTS
        System.String. Create-Password returns a string to be used as a passphrase.

        .EXAMPLE
        PS> Create-Password
        cayman-$hip-janet-$any0

        .EXAMPLE
        PS> Create-Password -Length 24
        jung!e-images-c0pper-wa!es

        .LINK
        https://github.com/nouselesstech/PowerShellHelpers

    #>


    param (
        $Length
    )

    $PwLength = 20
    if ($Length -gt 0) {
        $PwLength = $Length
    }
    $Output = ''

    try { 
        # Get Word List
        $WordListUri = "https://raw.githubusercontent.com/oprogramador/most-common-words-by-language/master/src/resources/english.txt"
        $RawList = Invoke-WebRequest -Method Get -Uri $WordListUri 
        $WordList = $RawList.Content.split("`n") | Where-Object { $_.Length -in @(4,5,6) }
        
        While($Output.Length -le $PwLength) {
            $Output += $WordList | Get-Random
            $Output += "-"
        }
        $Output = $Output -replace "-$", ""

        $NumReplaceList = 'eiot'.ToCharArray()
        $NumReplacer = $NumReplaceList | Where-Object { $_ -in $Output.ToCharArray() }
        $NumReplacer = $NumReplacer | Get-Random
        Switch ($NumReplacer) {
            "e" { $Output = $Output -replace 'e', '3'; break;}
            "i" { $Output = $Output -replace 'i', '1'; break;}
            "o" { $Output = $Output -replace 'o', '0'; break;}
            "t" { $Output = $Output -replace 't', '7'; break;}
        }

        $AlphaReplaceList = 'ahls'.ToCharArray()
        $AlphaReplacer = $AlphaReplaceList | Where-Object { $_ -in $Output.ToCharArray() }
        $AlphaReplacer = $AlphaReplacer | Get-Random
        Switch ($AlphaReplacer) {
            "a" { $Output = $Output -replace 'a', '@'}
            "h" { $Output = $Output -replace 'h', '#'}
            "l" { $Output = $Output -replace 'l', '!'}
            "s" { $Output = $Output -replace 's', '$'}
        }
    } catch {
        Write-Error $_
        # could not get word list
        $CharList = 'abcdefghijklmnopqrstuvwxyz'
        $CharList += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        $CharList += '1234567890'
        $CharList += '!@#$%^&*()-=_+[]{};:,./<>?'
        While($Output.length -lt $PwLength) {
            $Output += $CharList.ToCharArray() | Get-Random
        }
    }

    return $Output
}