Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Get-Credential this week.
When to use Get Credit
Anytime you need a credential object that is based on a username and password.
An authentication dialog box will prompt the user by default. You can prompt the user at command line with some host programs, such the PowerShell console.
How to use Get Credit
These commands use a credential object returned by the Get-Credential cmdlet to authenticate remote users so they can use Windows Management Instrumentation to manage the computer.
$c = Get-Credential
Get-WmiObject Win32_DiskDrive -ComputerName Server01 -Credential $c
The first command creates a credential object, and saves it in $c.
The second command uses credential objects in Get-WmiObject commands. This command provides information about the disk drives of the Server01 computer.
This command uses PromptForCredential to prompt the user to enter their password and user name. The $Credential variable is saved with the credentials.
$Credential = $host.ui.PromptForCredential(“Need credentials”, “Please enter your user name and password.”, “”, “NetBiosUserName”)
Alternative to the Get-Credential cmdlet, the PromptForCredential method can be used. PromptForCredential allows you to specify the caption, messages and user name that will appear in the message box.
This example shows how to modify a registry so that the user is prompted at a command line instead of using a dialog box.
Set-ItemProperty “HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds” -Name ConsolePrompting -Value $true
This command creates the ConsolePrompting registry record and sets its value as True. This command can be run by starting PowerShell using the “Run As Administrator” option.
To use a dialog box to prompt, set the value for the ConsolePrompting property to false ($false), or use the Remove – ItemProperty cmdlet.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.