Skip to content

Instantly share code, notes, and snippets.

@Goadstir
Created January 8, 2020 17:29
Show Gist options
  • Select an option

  • Save Goadstir/59832a51868589b7d464aa1c9034cecb to your computer and use it in GitHub Desktop.

Select an option

Save Goadstir/59832a51868589b7d464aa1c9034cecb to your computer and use it in GitHub Desktop.
PowerShell: Create and View Credential Objects
# create secure string from plain-text string
$secureString = ConvertTo-SecureString -AsPlainText -Force -String $password
Write-Host "Secure string:",$secureString
# convert secure string to encrypted string (for safe-ish storage to config/file/etc.)
$encryptedString = ConvertFrom-SecureString -SecureString $secureString
Write-Host "Encrypted string:",$encryptedString
# convert encrypted string back to secure string
$secureString = ConvertTo-SecureString -String $encryptedString
Write-Host "Secure string:",$secureString
# use secure string to create credential object
$credential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList $username,$secureString
Write-Host "Credential:",$credential
# show credential including clear text password :*( ...on purpose though
$credential.GetNetworkCredential() | fl *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment