Get-Credential powershell command

Get-Credential | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Credential.

When to use Get-Credential

Anytime you need to get a credential object based on a user name and password.

By default, an authentication dialog box appears to prompt the user. However, in some host programs, such as the PowerShell console, you can prompt the user at the command line by changing a registry entry.

How to use Get-Credential

These commands use a credential object that the Get-Credential cmdlet returns to authenticate a user on a remote computer so they can use Windows Management Instrumentation (WMI) to manage the computer:

$c = Get-Credential

Get-WmiObject Win32_DiskDrive -ComputerName Server01 -Credential $c

The first command gets a credential object and saves it in the $c variable.

The second command uses the credential object in a Get-WmiObject command. This command gets information about the disk drives on the Server01 computer.

Get-Credential powershell commands

Get-Credential powershell commands

 

This command uses the PromptForCredential method to prompt the user for their user name and password. The command saves the resulting credentials in the $Credential variable:

$Credential = $host.ui.PromptForCredential(“Need credentials”, “Please enter your user name and password.”, “”, “NetBiosUserName”)

The PromptForCredential method is an alternative to using the Get-Credential cmdlet. When you use PromptForCredential, you can specify the caption, messages, and user name that appear in the message box.

Get-Credential powershell command

 

This example shows how to modify the registry so that the user is prompted at the command line, instead of by using a dialog box:

Set-ItemProperty “HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds” -Name ConsolePrompting -Value $true

The command creates the ConsolePrompting registry entry and sets its value to True. To run this command, start PowerShell with the “Run as administrator” option.

To use a dialog box for prompting, set the value of the ConsolePrompting to false ($false) or use the Remove-ItemProperty cmdlet to delete it.

Get-Credential powershell command

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.