Powershell

Using the PowerShell Get-Credential Cmdlet and all Things Credentials

When the “Get-Credential” cmdlet in PowerShell gets executed, it opens a window to enter username and password. Any user can log into PowerShell without logging out of the current Windows session by entering a username and password. Moreover, it helps create “PSCredential” objects such as usernames and passwords. PSCredential objects are a secure way to pass and store the credentials to log in through PowerShell.

This write-up will present a guide to explain the mentioned query.

What is a “Get-Credential” Cmdlet in PowerShell?

Whenever the “Get-Credential” cmdlet gets executed in PowerShell, it displays the following window, which prompts the user to enter the credentials and log in as another user in PowerShell:

Get-Credential

The users can log in to PowerShell without going through the above interface. Instead, they need to create credentials to log in. Again, this is only possible through the “Get-Credential” cmdlet.

How to Use “Get-Credential” Command in PowerShell?

Follow the provided syntax to check out the usage of the “Get-Credential” command in PowerShell.

Step 1: Create a Secure String

At first, build a secure encrypted password by executing the below line of code:

$password = ConvertTo-SecureString 'MyPassword123' -AsPlainText -Force

In the above code:

  • First, initialize a variable “$password”, then assign the “ConvertTo-SecureString” to it.
  • Assign a string password to the “ConvertTo-SecureString” variable.
  • The “ConvertTo-SecureString” variable converts the standard text into a secure encrypted password.
  • Moreover, add the “-AsPlainText” parameter to convert a string password into plain text and the “-Force” parameter:

Execute the password assigned variable to check whether a password is created or not:

$password

Step 2: Create PSCredential Objects

Create the “PSCredential” objects, such as username and password:

$credential = New-Object System.Management.Automation.PSCredential ('linuxhint', $password)

According to the above code:

  • First, initialize a variable “$credential”, and assign “New-Object” to create new objects, such as username or password.
  • After that, assign “System.Management.Automation.PSCredential()” to the “New-Object” cmdlet to create new objects.
  • In the “System.Management.Automation.PSCredential()” cmdlet, first add the username and then specify the password assigned variable separated by a comma to it:

To check the newly created username, execute the below command:

$credential.Username

To view the created password, execute the below command:

$credential.GetNetworkCredential().Password

Step 3: Get the Created Credentials

Finally, the user can log in to the PowerShell without being prompted for credentials by executing the mentioned command:

Get-Credential -Credential $credential

According to the above code:

  • First, add the “Get-Credential” cmdlet. After that, add the “-credential” parameter and then assign the “$credential” variable to it:

It can be observed that the username and password were created and have been displayed in the console successfully.

Conclusion

The “Get-Credential” is a PowerShell cmdlet used to get security credentials, which is based on username and password. It asks the user to type in username and password credentials in the given window when executed. Moreover, the users can still log in without being asked for a password. This blog has observed detailed guidance about the “Get-Credential” cmdlet.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.