Tutorial by Examples

To prompt for credentials, you should almost always use the Get-Credential cmdlet: $credential = Get-Credential Pre-filled user name: $credential = Get-Credential -UserName 'myUser' Add a custom prompt message: $credential = Get-Credential -Message 'Please enter your company email address a...
The password in a credential object is an encrypted [SecureString]. The most straightforward way is to get a [NetworkCredential] which does not store the password encrypted: $credential = Get-Credential $plainPass = $credential.GetNetworkCredential().Password The helper method (.GetNetworkCrede...
To store and retrieve encrypted credentials easily, use PowerShell's built-in XML serialization (Clixml): $credential = Get-Credential $credential | Export-CliXml -Path 'C:\My\Path\cred.xml' To re-import: $credential = Import-CliXml -Path 'C:\My\Path\cred.xml' The important thing to remem...
$username = "[email protected]" $pwdTxt = Get-Content "C:\temp\Stored_Password.txt" $securePwd = $pwdTxt | ConvertTo-SecureString $credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd # Now, $credObject is having the credential...

Page 1 of 1