Skip to main content

PowerShell function to convert a secure string to plain text.

<#
    .SYNOPSIS
        Convert SecureString to plain text.

    .DESCRIPTION
        Convert SecureString to plain text.

    .PARAMETER SecureString
        Secure String to convert to plain text.

    .EXAMPLE
        Get-PlainText -SecureString $credential.Password
#>
function Get-PlainText
{
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline, Mandatory = $true)]
        [System.Security.SecureString]
        $SecureString
    )

    $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)

    try
    {
        return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
    }
    finally
    {
        [Runtime.InteropServices.Marshal]::FreeBSTR($bstr)
    }
}

Export-ModuleMember -Function Get-PlainText