Powershell and Secure/Encrypted strings

Hey there!

Sometimes you may have a need to encode or encrypt a string. This is usually neccessary for strings like passwords.

Let’s start with creating an encrypted string:

$password = "MostSecureOne123"

# Convert password to Secure String
$password_SecureString = ConvertTo-SecureString -String $password -AsPlainText -Force


# Convert a secure string to an encrypted string
$password_EncryptedString = ConvertFrom-SecureString $password_SecureString

Good, this can be now saved or further manipulated.

When you need to decrypt the string, use following:

# Convert from Encrypted string to Secure string
$password_SecureString = ConvertTo-SecureString -String $password_EncryptedString

# Convert from Secure string to plain text
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $env:username,$password_SecureString

$PlainPassword = $Credentials.GetNetworkCredential().Password

# Here it is!
$PlainPassword

 

Let me know whether this was useful to you, or not.

 

Cheers!

Robot(ICT) guy

Published by

Lukas Vu

I started in IT with my own business since high school providing automated and centralized hosting solutions to end customers. Nowadays I'm focusing on corporate area, analyzing, managing and improving customer's IT environments. My main focus nowadays is automation and architectural improvements.

Leave a Reply

Your email address will not be published. Required fields are marked *