private/get/Get-InstancePassword.ps1

function Get-InstancePassword
{
    [CmdletBinding(PositionalBinding = $true)]
    param(
        [Parameter(Mandatory = $true)]
        [string] $InstanceId
    )

    $result = $null
    $keyName = Get-EC2Instance -InstanceId $InstanceId | Select-Object -ExpandProperty 'Instances' | Select-Object -ExpandProperty 'KeyName'

    if (Test-Path -Path "$keyName.pem")
    {
        Write-Host "Key file '$keyName.pem' found." -ForegroundColor DarkGray

        $password = Get-EC2PasswordData -InstanceId $InstanceId -Decrypt -PemFile "$keyName.pem"

        $result = $password.PasswordData
    }
    else
    {
        Write-Host "Key file '$keyName.pem' not found." -ForegroundColor DarkGray
    }

    return $result
}