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
}

# SIG # Begin signature block
# MIIDOAYJKoZIhvcNAQcCoIIDKTCCAyUCAQExDzANBglghkgBZQMEAgMFADCBmwYK
# KwYBBAGCNwIBBKCBjDCBiTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63
# JNLGKX7zUQIBAAIBAAIBAAIBAAIBADBRMA0GCWCGSAFlAwQCAwUABEAuEJw382GK
# 28KFvq7T85twWj6XIi15vz/0efJCZ1GV5k1cAcbHsGBhNlLKtzZaSBpLuIgQpkiP
# wDNKjGzruPWRoIIBRTCCAUEwgeigAwIBAgIUJUrhIyYe0Ijmq8lkl4K8jyNrTDUw
# CgYIKoZIzj0EAwIwIDEeMBwGA1UEAwwVY29ubm9yQG5vdXNlbGVzcy50ZWNoMCAX
# DTI0MDUwNTA0MDUyOVoYDzIyMjIxMjIyMDAwMDAwWjAgMR4wHAYDVQQDDBVjb25u
# b3JAbm91c2VsZXNzLnRlY2gwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARFc0rS
# N95xEemn7/TlV7WGrIJs0xXd0L7IdY0nu5hVqaR9FJ7DD/lV/8tgAPgOw8756FTv
# r3889XN3rSir8uLlMAoGCCqGSM49BAMCA0gAMEUCIQD9FOnUCgckJaGI3Hgqkqzn
# n7cRN8O9FESkoAKapUA/8gIgD6P7+V2DIwzPSBPPY7W07BZ3X2r29L7KHZ8ZUcrS
# m2wxggEmMIIBIgIBATA4MCAxHjAcBgNVBAMMFWNvbm5vckBub3VzZWxlc3MudGVj
# aAIUJUrhIyYe0Ijmq8lkl4K8jyNrTDUwDQYJYIZIAWUDBAIDBQCgfjAQBgorBgEE
# AYI3AgEMMQIwADAZBgk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