title: PowerShell - Decode System.Security.SecureString to readable password
body: <p>I want to decode the password from a System.Security.SecureString to a readable password.</p>
  
  <pre><code>$password = convertto-securestring "TestPassword" -asplaintext -force
  $credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
  </code></pre>
  
  <p>This code part works fine, I can use the $credentials object.
  But later in my code I need the password in a readable format. Because a methode needs the password in readable string. So I must decode the password back.</p>
  
  <p>How it is possible to decode the password from the $credentials object?</p>
  
  <p><em><strong>Update</em></strong></p>
  
  <p>Not working:</p>
  
  <pre><code>$password = convertto-securestring "TestPassword" -asplaintext -force
  $credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
  
  $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($credentials.password)
  $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
  [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
  $result 
  </code></pre>