Skip to content

Instantly share code, notes, and snippets.

@blinds52
Forked from magnetikonline/example.ps1
Created April 15, 2017 18:12
Show Gist options
  • Save blinds52/0da4f64c3366c9bf5f4ed156792367c1 to your computer and use it in GitHub Desktop.
Save blinds52/0da4f64c3366c9bf5f4ed156792367c1 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