Private/Passphrases/_NewUserPassphrase.ps1

function _NewUserPassphrase {
    <#
    .Synopsis
       Generate a new password that meets standard password requirements.
    .DESCRIPTION
       Generates a password using a common word list (_GetWord). By default, the function returns 3 words separated by dashes with a number after the second word.
    #>

    param(
        [parameter(mandatory = $true)]
        [int]$Words,

        [parameter(mandatory = $true)]
        [char]$Separator
    )

    try {
        $i = $Words
        $GeneratedPassword = ''
        Do {
            $i--
            $GeneratedPassword += "$(_GetWord)-"

        } until ($i -eq 0)
    }
    catch {
        $MessageSplat = @{
            MessageText  = "Unable to get generated passphrase from _GetWord.`n$_"
            MessageIcon  = 'Hand'
            ButtonType   = 'OK'
            MessageTitle = 'Error'
        }
        $Blackhole = _ShowMessageBox @MessageSplat
        break
    }
    try {
        # Change the generated password to have uppercase and a number
        $PWSplit = $GeneratedPassword.split('-')
        $CorrectPassword = ''

        # This foreach changes the case of the first letter of each word and then puts the '-' back between words
        # After the 2nd word, it adds a random single digit number
        $i = 0
        $SpotForNumber = Get-Random -Minimum 1 -Maximum ($Words + 1)
        foreach ($Word in $PWSplit) {
            $i ++

            # .substring() shouldn't be giving an error, but it is. suppressing again.
            # ERROR: Error generating initial password. Unable to modify password string to include uppercase and number. Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string. 1/1/1601 12:00:00 AM 0 (0x0000)

            $ErrorActionPreference = 'SilentlyContinue'
            $Word = (Get-Culture).TextInfo.ToTitleCase($Word)
            $ErrorActionPreference = 'Stop'
            $CorrectPassword += $Word

            # Get-Random returns 1 less than the maximum
            if ($i -eq $SpotForNumber) {
                $CorrectPassword += Get-Random -Minimum 0 -Maximum 10
            }
            if ($i -lt $Words) {
                $CorrectPassword += "$Separator"
            }
        }
    }
    catch {
        $MessageSplat = @{
            MessageText  = "Unable to create passphrase.`n$_"
            MessageIcon  = 'Hand'
            ButtonType   = 'OK'
            MessageTitle = 'Error'
        }
        $Blackhole = _ShowMessageBox @MessageSplat
    }
    return $CorrectPassword
}