In this blog post I will carry out how to decrypt a secure string. A secure string is a string that is saved in a non-readable format to temporarily store passwords and strings. Let’s start.
For testing purposes, store a password as a secure string.
$password = ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force
Next, we will use a somewhat cryptic code to decrypt this.
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password)
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
Here is the entire code in action:

Fine that’s it.
Categories: PowerShell
Why not use ConvertFrom-SecureString, especially in PowerShell 7 where it can return the original plaintext string.
LikeLike
Thank you! Didn’t know there a changes in PowerShell 7.
LikeLike