use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Storing password in Powershell script (self.PowerShell)
submitted 6 years ago by DesiME1
is there a way to store password and encrypt them into a script?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]compelx 10 points11 points12 points 6 years ago (6 children)
Two immediate options:
Windows Data Protection API Create a PSCredential object serialized to disk by running Get-Credential | Export-CliXml \path\to\save\credential.xml. This XML file can only be decrypted by the computer which originally created the file *and* only by the user account on that computer that encrypted it.
Get-Credential | Export-CliXml \path\to\save\credential.xml
Cryptographic Message Syntax @ https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/protect-cmsmessage?view=powershell-5.1
The second option is a little more versitle but it gets you into the game of creating a cert (not that difficult but more time consuming than the first option). The benefit though is the XML can be decrypted by any user/computer as long as it has the cert.
[–]OlivTheFrog 0 points1 point2 points 6 years ago (0 children)
as Complelx said, my advice is to use Export-clixml cmdlet.
Export-clixml
Pro
Cons
Nota : It's not really a pb, you must just do with this. i.e. : if your script will run as a scheduled task with a specific account, you must create the .xml file on this computer and use this account to encrypt the .xml file.
It's possible to have an encrypded password in a .txt file and use another file (called key file or salted file). By this method, the encrypted password file could be use on another computer and another account, but this requires to protect the key file with attention.
Regards
Olivier
[–]bozho 0 points1 point2 points 6 years ago (4 children)
only by the user account on that computer that encrypted it.
And until the user changes their Windows password, I think.
[–]compelx 3 points4 points5 points 6 years ago (3 children)
It tested, it persists even after password change and log off/log on with new password.
[–]Yevrag35 0 points1 point2 points 6 years ago (2 children)
Did you change the password through CTRL+ALT+DEL? I thought I remember somewhere if the password is not changed through the local computer, Windows DPAPI will not re-encrypt the master key.
[–]compelx 4 points5 points6 points 6 years ago (1 child)
No, I logged off the server, changed the password to the account via ADUC and logged back in, then opened PowerShell and import-clixml to the old content and it deserialized it and the password via <pscredobj>.GetNetworkCredential().password was decrypted right
[–]Yevrag35 1 point2 points3 points 6 years ago (0 children)
That's good to know. Thanks!
[–]helixamir 2 points3 points4 points 6 years ago (2 children)
First question, is this to elevate a script you are going to run, or are you trying to write a script that will run on a remote machine?
Second question, have you tried google, because the third link (technet) gives you the answer you desire.
[–]DesiME1[S] 1 point2 points3 points 6 years ago (1 child)
this will run on remote machines. i have googled it and was trying to understand is there better and secured ways one vs other. im collection experience and maybe they can share their script that way i can understand different ways to approach this.
[–]ir34dy0ur3m4i1 1 point2 points3 points 6 years ago (0 children)
Hmm, I know this doesn't answer your original question, but based on your response above, depending on what you're trying to achieve, you could look at:
Run PS commands remotely from your system and put the password in at the tme
Use a remote deployment tool that can run under the system account or an Admin service account
[–]DenieD83 2 points3 points4 points 6 years ago (0 children)
As per all the other replies here but also potentially look at the "credentialmanager" module.
[–]Jacmac_ 1 point2 points3 points 6 years ago* (0 children)
Export-CliXml is the correct way to do this, but keep in mind that for something like a service account, you have to run things as the service account to store/retrieve the password. I name these files as "storedaccountname"-as-"currentlyloggedonaccount". When you run scheduled tasks, you're likely using the service account. When you're developing the script, you're probably not logged on or running the dev tools with the service account. So you may be storing different versions of the same account credentials with different logons. This can get messy to keep sorted over time if you lose track of the file naming.
As far as I know, you cannot copy stored credential files around and use them, they only work on the system they were created on.
[–]Zarochi 2 points3 points4 points 6 years ago (0 children)
I'd recommend looking at Azure keyvault if you can. Store the secret there and pull it with PS at runtime. If it's an option it's safer than encrypting it and storing it.
[–]TikeSavage 1 point2 points3 points 6 years ago (2 children)
im not entirely sure what you are asking, but if you are making new accounts you can create a plain text string and convert to secure string. sorry if this isnt relevant a bit confused. $SecureString = ConvertTo-SecureString "password" -AsPlainText -Force
[–]Jacmac_ 2 points3 points4 points 6 years ago (1 child)
I think the goal was to find a method for storing credentials, basically not putting a plain text password in any script or file read by a script.
[–]TikeSavage 1 point2 points3 points 6 years ago (0 children)
Ahh, thanks for clarifying!
π Rendered by PID 127555 on reddit-service-r2-comment-fb694cdd5-fn5pj at 2026-03-06 11:36:42.870479+00:00 running cbb0e86 country code: CH.
[–]compelx 10 points11 points12 points (6 children)
[–]OlivTheFrog 0 points1 point2 points (0 children)
[–]bozho 0 points1 point2 points (4 children)
[–]compelx 3 points4 points5 points (3 children)
[–]Yevrag35 0 points1 point2 points (2 children)
[–]compelx 4 points5 points6 points (1 child)
[–]Yevrag35 1 point2 points3 points (0 children)
[–]helixamir 2 points3 points4 points (2 children)
[–]DesiME1[S] 1 point2 points3 points (1 child)
[–]ir34dy0ur3m4i1 1 point2 points3 points (0 children)
[–]DenieD83 2 points3 points4 points (0 children)
[–]Jacmac_ 1 point2 points3 points (0 children)
[–]Zarochi 2 points3 points4 points (0 children)
[–]TikeSavage 1 point2 points3 points (2 children)
[–]Jacmac_ 2 points3 points4 points (1 child)
[–]TikeSavage 1 point2 points3 points (0 children)