Public/Get-RandomPassword.ps1

using namespace System.Security.Cryptography

function Get-RandomPassword {
    <#
        .SYNOPSIS
        Generates a cryptographically secure random password.

        .DESCRIPTION
        Generates a secure random password of the specified length. The password
        is encoded in Base64 and derived from a buffer of cryptographically secure
        random bytes generated by the System.Security.Cryptography.RandomNumberGenerator
        class.

        .PARAMETER Length
        Specifies the length of the generated password. The default length is 64 characters.
        The length must be between 8 and 256 characters.

        .INPUTS
        None. You can't pipe objects to Get-RandomPassword.

        .OUTPUTS
        System.String. A randomly generated password in Base64 format of the specified length.

        .EXAMPLE
        PS> Get-RandomPassword

        Generates a random password of the default length (64 characters).

        .LINK
        https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.randomnumbergenerator
    #>

    [OutputType([string])]
    [CmdletBinding()]
    param(
        [ValidateRange(8, 256)]
        [Parameter(Position = 0)]
        [int] $Length = 64
    )

    process {
        # Base64 encoding encodes every 3 bytes of input data into 4 characters
        # of output data. The required length of the password can be computed by
        $Size = [Math]::Floor($Length * 3 / 4)
        $Buffer = [byte[]]::new($Size)

        [RandomNumberGenerator]::Fill($Buffer)
        $Password = [Convert]::ToBase64String($Buffer)
        Write-Output $Password
    }
}