Skip to content

Instantly share code, notes, and snippets.

@rbramwell
Forked from magnetikonline/example.ps1
Created April 27, 2016 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbramwell/ac59271d9477bbab52443a8b0869eaf8 to your computer and use it in GitHub Desktop.
Save rbramwell/ac59271d9477bbab52443a8b0869eaf8 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