Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Created December 15, 2015 06:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save magnetikonline/a6db9c3835a65685df86 to your computer and use it in GitHub Desktop.
Save magnetikonline/a6db9c3835a65685df86 to your computer and use it in GitHub Desktop.
Creating a PowerShell PSCredential object with username/password using secure/encrypted strings.
Set-StrictMode -Version Latest
$PASSWORD = "mypassword"
# create secure string from plain-text string
$secureString = ConvertTo-SecureString -AsPlainText -Force -String $PASSWORD
Write-Host "Secure string:",$secureString
Write-Host
# convert secure string to encrypted string (for safe-ish storage to config/file/etc.)
$encryptedString = ConvertFrom-SecureString -SecureString $secureString
Write-Host "Encrypted string:",$encryptedString
Write-Host
# convert encrypted string back to secure string
$secureString = ConvertTo-SecureString -String $encryptedString
Write-Host "Secure string:",$secureString
Write-Host
# use secure string to create credential object
$credential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList "myusername",$secureString
Write-Host "Credential:",$credential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment