By using a key file we can encrypt our password.
$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file C:\Password\PWEncrypt.key
This will generate a key file.
After that run the below script to encrypt any password. It will save encrypted password value is Credentials.txt file which can be decrypted later.
(get-credential).Password | ConvertFrom-SecureString -key (get-content "$PSScriptRoot/PWEncrypt.key"
) | set-content "$PSScriptRoot/Credentials.txt"