#PSTip Get-Credential at the command line

PowerShell’s Get-Credential cmdlet lets us create a secure credential object for a specified user name and password using a UI dialog:

PS> Get-Credential shay

There’s a way of replacing the UI and collect the credentials via the command line. You need to be an administrator to do that and the console must be elevated (e.g “Run as Admin”).

The change involves adding a registry value to the HKLM hive:

$key = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds"
Set-ItemProperty -Path $key -Name ConsolePrompting -Value $true

You add the ConsolePrompting value to the above path and set its data to $true. From now on, all Get-Credential calls will look like this:

PS> Get-Credential shay

Windows PowerShell Credential Request
Enter your credentials.
Password for user shay: ********

To bring back the UI dialog, set the value to $false or remove it all altogether. Note that this trick doesn’t have any effect in the ISE.

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts