13

I am writing a project where I run a PowerShell script and create a new PSSession as follows:

PowerShell.exe -Command enter-pssession myUser -credential userName 

When I run this, it opens a dialog to prompt the user for a password. However, I would prefer for the user to be able to enter the password along with the rest of the above line instead of having to be bothered with the prompt. I'm very new to PowerShell, and everything I've found in docs only gives ways to bring the prompt up for the password. Is this something that is possible to accomplish in PowerShell? Thanks!

3 Answers 3

15
+50

Rahul gave you an answer that lets you avoid the password prompt altogether but at the risk of entering the user's password in clear text. Another option would be to allow the user to fill in their password in the popup box once but then save it so they never need to re-enter it.

To popup a prompt for username and password and save the result to a file:

 Get-Credential | Export-Clixml "mycredentials.xml" 

Then you can either read that into a variable:

$cred = Import-Clixml "mycredentials.xml" new-pssession -computername <computer> -credential $cred 

or combine the two commands:

new-pssession -computername <computer> -credential (Import-Clixml "mycredentials.xml") 

When you save the credentials to a file this way the password is securely encrypted using the currently logged-on user's login credentials.

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting 'new-pssession : This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system. ' error. Pls help. I have searched for solutions online but nothing worked.
How PowerShell knows to which device it should connect if multiple devices share the same IP address? If I am not mistaken, the Enter-PSSession cmd uses only the IP address as a parameter, not the device name or any other more specific addressing.
11

Yes, you can provide password while opening a new-pssession like below

$passwd = convertto-securestring -AsPlainText -Force -String <your password> $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\User",$passwd $session = new-pssession -computername <computer> -credential $cred 

2 Comments

Add Read-Host -AsSecureString to this, and the password can be entered on the commandline without a popup, and without showing it as plain text.
How PowerShell knows to which device it should connect if multiple devices share the same IP address? If I am not mistaken, the Enter-PSSession cmd uses only the IP address as a parameter, not the device name or any other more specific addressing.
1

The existing answers are not up to date, this is how it currently works:

$password = ConvertTo-SecureString "password" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ("username", $password) $session = Enter-PSSession -computername "computername" -credential $cred 

Here is some documentation about credentials from Microsoft: https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/add-credentials-to-powershell-functions?view=powershell-7.2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.