Saving Credentials

by Jun 28, 2016

Here is a safe way of saving credentials to a file:

$CredPath = "$home\Desktop\mycred.xml"
Get-Credential | Export-Clixml -Path $CredPath 

This code generates the file mycred.xml on your desktop. The password is encrypted with your identity and the machine identity. Only you will be able to read in the credential, and only if done so on the machine where it was saved:

$CredPath = "$home\Desktop\mycred.xml"
$cred = Import-Clixml -Path $CredPath 
$cred

You can then use the credential in $cred wherever a cmdlet supplies a -Credential parameter. Example:

Get-WmiObject -Class Win32_BIOS -ComputerName server1 -Credential $cred

Twitter This Tip! ReTweet this Tip!