IIT.Powershell.PlainText.psm1
function ConvertTo-PlainText() { <# Created by Andrej Licar, IIT .Synopsis Converts secure string to plain text .Description Returns plain text from secure string (e.g. created with ConvertTo-SecureString <plaintext> or Password from PSCredential object) .Notes Author : Andrej Licar, IIT ltd. <andrej@iit.si> Version : 1.0 - 2019/01/13 - Initial release .Inputs SecureString .Outputs String .Parameter SecureString Specifies the secure string to convert to plain text .Example Gets the plain text from a secure string PS C:\> ConvertTo-PlainText -SecureString (ConvertTo-SecureString "password") .Example Gets the plain text from a secure string PS C:\> ConvertTo-PlainText (ConvertTo-SecureString "password") .Example Gets the plain text from a secure string by piping it in PS C:\> ConvertTo-SecureString "password" | ConvertTo-PlainText .Example Gets the credentials from a propmt and converts the password property of the object to plain text PS C:\> $credential = Get-Credential PS C:\> ConvertTo-PlainText -SecureString $credential.Password #> [CmdletBinding(SupportsShouldProcess=$true)] Param( [Parameter( Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, HelpMessage="Secure string to convert to plain text", Mandatory=$true )] [SecureString] $SecureString ) # define the pointer of text in memory $ptr = [IntPtr]::Zero # define the output plain text $text = $null # define the marshal shortcut $marshal = [System.Runtime.InteropServices.Marshal] if ($PSCmdlet.ShouldProcess("SecureString", "Convert the secure string ti plain text")) { try { # get the pointer in memory of the secure string $ptr = $marshal::SecureStringToCoTaskMemUnicode($SecureString) # read the plain text from the memory $text = $marshal::PtrToStringUni($ptr) } finally { # test if the pointer was defined (the string in memory was present) if ($ptr -ne [IntPtr]::Zero) { # clear the pointer $marshal::ZeroFreeCoTaskMemUnicode($ptr) } } } # return the plain text $text } New-Alias -Name "plain" -Value "ConvertTo-PlainText" -Description "Convert Secure String to Plain text" New-Alias -Name "ctp" -Value "ConvertTo-PlainText" -Description "Convert Secure String to Plain text" Export-ModuleMember -Function "ConvertTo-PlainText" -Alias "*" |