Private/Get-PlainPasswordFromCredential.ps1

<#
.SYNOPSIS
Extract the plain-text password from a PSCredential object.
 
.DESCRIPTION
Converts the SecureString password in a PSCredential to a plain-text string using Marshal interop. The BSTR is zeroed after extraction.
 
.PARAMETER Credential
The PSCredential from which to extract the password.
 
.EXAMPLE
PS> Get-PlainPasswordFromCredential -Credential (Get-Credential)
Returns the plain-text password string.
#>

function Get-PlainPasswordFromCredential {
    param(
        [Parameter(Mandatory)]
        [pscredential]$Credential
    )
    $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)
    try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) }
    finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) }
}