Get-Credential
Use Get-Credential to securely prompt for a username and password and store them as a PSCredential object for cmdlets that need authentication.
Documentation: Microsoft Learn — get-credential
Syntax
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Examples
Prompt and store the credential
Show reference output
Illustrative output · values vary by environment
cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: User: CONTOSO\admin Password for user CONTOSO\admin: ********
Pre-fill the username and message
Show reference output
Illustrative output · values vary by environment
UserName: CONTOSO\admin Password: System.Security.SecureString
Use the credential on a remote computer
Show reference output
Illustrative output · values vary by environment
Status Name DisplayName ------ ---- ----------- Running Spooler Print Spooler Stopped WinRM Windows Remote Management
Parameters
Parameter definitions:
-Credential— An existing PSCredential to edit, or a username string to pre-fill.-UserName— Pre-fills the username field so the user only types a password.-Message— Text shown above the prompt, explaining why credentials are needed.-Title— Title of the credential prompt window.
Never put a password in your script
A PSCredential keeps the password as a SecureString in memory. The moment you write a password as plain text in a .ps1 file, anyone who can read the file owns the account. Prompt with Get-Credential for interactive runs.
For scheduled or unattended scripts, store the credential once with Export-Clixml under the account that will run the job, then import it at runtime. An exported credential can only be decrypted by the same user on the same computer.
Read the pieces back with $cred.UserName and $cred.GetNetworkCredential().Password. The second call returns plain text, so use it only at the exact moment a cmdlet needs it and never write it to output or logs.
Build credentials from a secure string
Read a password with Read-Host -AsSecureString
Updated 25 October 2026. Browser outputs are fictional; check the installed module help for your environment.
Recap
Get-Credential Use Get-Credential to securely prompt for a username and password and store them as a PSCredential object for cmdlets that need authentication.
Next: Read Host