r/PowerShell • • Nov 13 '25

Question Encrypting and decrypting a string with Powershell using a text password

Hi all,

what is the best way to perform password based encryption and decryption with Powershell?

Here's some context:

I have a powershell utility script that performs some API call to a server. These calls include a token, which at the moment is for convenience stored in plaintext inside the script. Since I need to share this script with other possibly untrusted users, I would like to store this token encrypted, so that the user launching the script needs to insert the right key (password) to decrypt the token and successfully execute the API calls.

In short, I would like to:

  • Take a plaintext string and encrypt it using a text password
  • Make the inverse operation
  • All of this using only Powershell v 5.1

I think it shouldn't be hard to do it, but I couldn't find a way on my own looking on the web, can anyone help me with this? Does it even make sense or is there a better way to obfuscate the token and request authorization for launching the script?

Much appreciate anyone taking the time!

17 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/gblang Nov 14 '25

Yes I was trying to use ConvertFrom-SecureString cmdlets with the -Key param but first I wasn't able to generate any valid key and also I would like this key to be generated from a text password so that I don't have to pass around an additional file with the key. Do you know a quick way to do this?

2

u/purplemonkeymad Nov 14 '25

You could just sha256 the password to generate a key.

However your best course of action will always be to have the api do something like oauth2 to get a token for each user, that way you don't ever need to store it.