Forum Discussion
Secure Way to store lots of credentials using powershell
- Aug 07, 2020
There are many ways to encrypt your password and store them in text file, csv, database, Windows credential vault and etc., In the end you still need to decrypt it to be able to use it.
So, my best bet would be to use Azure Key Vault.
Get-Credential | Export-Clixml -Path "C:\Users\username\Desktop\Cred.xml"
and then call them later using below in the required cmdlet:-
"-Credential (Import-Clixml "C:\Users\username\Desktop\Cred.xml")"
First of all, thank you for your answer!
I like your way of exporting it into a .xml file. 🙂
But the thing is that I have to enter the credentials into a popup window. I have too many Credentials to be even remotely able to do it this way...
Is it somehow possible that I could put all of the credentials into a .csv (for example) with the username as plain text and the password encrypted using SecureString so I have all them in one file?
Cheers
Martin
- __Martin__Jul 29, 2020Copper Contributor
Well I kind of found a solution:
#set credentials #set credentials path $CredPath = "C:\Scripts\Certify\mycreds.xml" #How many Credential Windows are going to show $creds = @{ Local1 = Get-Credential -Message LocalAccount1 Local2 = Get-Credential -Message LocalAccount2 Local3 = Get-Credential -Message LocalAccount3 } #export credentials which have been typed in manually as xml $creds | Export-Clixml -Path $CredPath #get credentials #import credentials $creds = Import-Clixml -Path $CredPath #list credentials $creds.Local1 $creds.Local2 $creds.Local3 # example Get-WmiObject -Class Win32_BIOS -ComputerName server01 -Credential $creds.Local1
My only problem now: there are still annoying popup windows where I have to enter all of this after one another
Can anyone help me with that?
Thanks in advance
Martin
- RobBrattonUPMCApr 14, 2025Copper Contributor
You can create a PSCredential object using New-Object and providing the username and Secure String password.
$username = "john.doe" $password = "SecureP@ssw0rd" $securePassword = ConvertTo-SecureString $password -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
Source: https://powershellfaqs.com/create-a-credential-object-in-powershell/
- DeepakRandhawaJul 29, 2020Iron Contributor
you can create an xml file for each set of credentials by first storing the creds in plain text in a csv file, using below :-
$file = Import-Csv "C:\temp\file.csv"
foreach($entry in $file){
$output=@()
$identity=$entry.username.split("@")[0]
$Username = $entry.username
$Password = $entry.password
$SecurePassword = convertto-securestring $Password -asplaintext -force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
$output=$credentials
$output | Export-Clixml -Path "C:\temp\$identity.xml"
}
You can create a single xml file as well for all creds however i don't know if there will be an easy way to use that file to feed credentials back into a PS session.