Powershell : Convert SecureString to Plain Text

You can force user to enter password from Powershell script using Read-Host cmdlet, and you can mask the password string by setting a parameter -asSecureString.

$password = Read-Host "Enter Password" -asSecureString

You can use this Secure String password wherever the password is needed as Secure String password, but you can not pass this SecureString value where the password required as Plain Text. This below powershell script converts a System.Security.SecureString object (secure string) to Plain Text (actual password string).

#SecureStringToPlainText.ps1
$password = Read-Host "Enter Password" -asSecureString
# Create a "password pointer"
$PwdPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
# Get the plain text version of the password.
$PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PwdPointer)
# Free the pointer.
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PwdPointer)
"Your have entered this password: " + $PlainTextPassword
Convert SecureString to Plain Text in Powershell

Advertisement

Leave a Comment