Once PsCredential
is captured with Get-Credential
cmdlet:
$Credential = Get-Credential
The $Credential
variable has two properties:
Username
Password
The type of Password
property is SecureString
:
$Credential.Password.GetType() --> SecureString
so it is encrypted. Now we can use these two cmdlets to transfer/save the password as a normal string without revealing the actual password:
ConvertFrom-SecureString
ConvertTo-SecureString
Let's say that we have provided Username as myuser
and Password as mypassword
to the prompt after Get-Credential
popup.
This below will convert the password to encrypted String
object:
$Credential.Password | ConverFrom-SecureString
# Output
01000000d08c9ddf0115d1118c7a00c04fc297eb01000000f9ae07907bbc82458b25fd92b2dae62e0000000002000000000003660000c000000010000000e1d56e48520215b461e09a24c53375660000000004800000a0000000100000008eeb7cf1234bac1806b5b624f4dcef0a18000000c8062d946ab9605dfcaaba9a20d7a7486c54010451c2dd0714000000ce000956b190d080f2c5eae9e159a78916d88e98
you can now save this output to a file and read it with Get-Content
later or save it to a PowerShell
variable:
$EncryptedPassword = $Credential.Password | ConverFrom-SecureString
you can convert this normal String
back to SecureString
using `ConvertTo-SecureString:
$Password = $EncryptedPassword | ConvertTo-SecureString
and then construct new PsCredential
object:
$CredentialNew = New-Object System.Management.Automation.PsCredential('myuser', $Password)
I hope this is what you are after.