PowerShell Genie

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.

Command 4 min read PowerShell 7 on Windows

Documentation: Microsoft Learn — get-credential

Syntax

1PS> Get-Credential [[-Credential] <PSCredential>] [-UserName <String>] [-Message <String>] [-Title <String>] [<CommonParameters>]

Try it

Windows PowerShell

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.

Interactive simulation · fictional data

Examples

Prompt and store the credential

1PS> $cred = Get-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

1PS> $cred = Get-Credential -UserName 'CONTOSO\admin' -Message 'Enter domain admin credentials'
Show reference output

Illustrative output · values vary by environment

UserName: CONTOSO\admin
Password: System.Security.SecureString

Use the credential on a remote computer

1PS> Invoke-Command -ComputerName SRV01 -Credential $cred -ScriptBlock { Get-Service }
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.

Microsoft Learn — get-credential

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
Genie guideGet-Credential

Get-Credential prompts for credentials without ever putting a password in plain text on screen, and returns a PSCredential object you can pass to Invoke-Command, New-PSSession, and other cmdlets. The console below is pre-loaded — run it, then change a value and run it again.