Public/Get-PlaintextFromSecureString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Get-PlaintextFromSecureString {
    <#
    .SYNOPSIS
    Extract the plaintext content from a SecureString

    .PARAMETER SecureString
    Sensitive content

    .EXAMPLE
    Get-PlaintextFromSecureString -SecureString $Password
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [Security.SecureString]
        $SecureString
    )

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
    [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
}