Powershell

Using PowerShell Credentials without Being Prompted for a Password

The “Get-Credential” cmdlet, when executed in PowerShell, opens a prompt window that asks to enter a username and password. Entering the login credentials permits the user to log in to PowerShell. The credentials are known to be a ubiquitous object in PowerShell. However, another user can also log in to PowerShell as a different user using the PSCredential object without being prompted for a password and logging out of the current Windows session.

This post will overview a procedure to rectify the stated query.

How to Use PowerShell Credentials Without Being Prompted for a Password?

We will use the “PSCredential objects” to use the PowerShell credentials without being prompted for a password. The “PSCredential objects” is a great method to store the credential and then log into various services.

Normally, when the “Get-Credential” gets executed, the PowerShell prompt appears for a password:

> Get-Credential

Here, the given command gets security credentials based on username and password:

Now, let’s find a solution to use the PowerShell credentials without being prompted for a password.

Step 1: Create a Secure String

First of all, create a secure string in order to safely store the password in it:

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

According to the given command:

  • First of all, use the “ConvertTo-SecureString” alongside the string password and assigned it to a variable.
  • $Password” variable to convert the string into a secure password.
  • Moreover, use the “-AsPlainText” parameter to convert the string password as plain text:

The secure string password has been created.

Let’s verify whether the password was securely created by accessing the value of the password variable:

> $password

It can be observed from the output that the password was successfully created and stored as a secure string.

Step 2: Create a PSCredential Object

Let’s define a “PSCredential” object to create a new username and password:

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

Here:

  • Firstly, use the “New-Object” cmdlet and assign the “System.Management.Automation.PSCredential” object type and store it in the variable.
  • PSCredential() constructor, which accepts username and password enclosed within parentheses:

Verify whether a user was created by viewing the username. For that reason, run the mentioned command:

> $credential.Username

In order to get the user password, execute the below command:

> $credential.GetNetworkCredential().Password

Step 3: Use the Credentials Without Being Prompted for a Password

After creating user credentials, now, let’s use them without opening the password prompt:

> Get-Credential -Credential $credential

As it can be seen that the credentials have been used without the password prompt.

Conclusion

To use the PowerShell credentials without being prompted for a password, first create a secure string password using the “ConvertTo-SecureString”. After that, create a “PSCredential” object using the “New-Object System.Management.Automation.PSCredential ()” command. Then, use the credentials without being prompted. This post has provided a comprehensive guide to use the PowerShell credentials without being prompted for a password.

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.