How To Encrypt Password Using PowerShell

In this blog, I would like to share the steps to encrypt the password in text file using PowerShell.

I got a requirement where we need to encrypt the password in text file and read text file in powershell script to create the sharepoint connection. 

Here is the code to generate password in encrypted format

$password = Read-Host -Prompt "Please enter your password" -AsSecureString
$bytes = ConvertFrom-SecureString $password
$bytes | out-file .\MyPassword.txt

Once you run this script it will prompt a window and ask for password so you can just type your password which you want to encrypt. After password is entered press the ok button. It will generate an encrypted password in a text file with name MyPassword.txt  in the below folder path - "C:\Sachin\POC\PnPEncryptPass"

When we open the text file to see our password is encrypted and we can refer to this password-protected file in any script while creating sharepoint connection.

Summary

In this blog, we have learned how to encrypt the password in text file using PowerShell.