43

How to create a instance of PSCredential that has no password? (Without manually filling out a Get-Credential dialog with no password, this is for unattended running.)

Things I tried:

  1. $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) Error: Cannot process argument because the value of argument "password" is null

  2. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) Error: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.

  3. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "" -AsPlainText -Force)) Error: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is an empty string.

1

2 Answers 2

63

Solution:

$mycreds = New-Object System.Management.Automation.PSCredential 
              ("username", (new-object System.Security.SecureString))
4
  • hi @EugeneO hope u can assist me with this stackoverflow.com/questions/9290830/… tks
    – JackyBoi
    Commented Feb 15, 2012 at 9:32
  • 4
    PowerShell is too convoluted for there to be such a thing as a dumb question.
    – jpmc26
    Commented Mar 30, 2017 at 19:37
  • 2
    Annoying seems you must handle the two cases with and empty and a filled password. ConvertTo-SecureString is hilariously useless: "Cannot bind argument to parameter 'String' because it is an empty string." well gee.
    – Svend
    Commented Sep 7, 2017 at 13:42
  • this is a bad idea Commented Feb 24, 2022 at 16:58
-2

Use the Get-Credential cmdlet, when the credential dialog appears, don't type a password

$cred = Get-Credential pebrian27
$cred.GetNetworkCredential() | select UserName,Password

UserName   Password
--------   --------
pebrian27
5
  • 4
    -1 - A dialog coming up and you not entering something is not the context of what the OP was asking. And the OP had given a proper answer as well, no need for this answer.
    – manojlds
    Commented Jul 27, 2011 at 6:55
  • 1
    There might be more than one solution and I don't see where the OP specifically asked not to use a cmdlet.
    – Shay Levy
    Commented Jul 27, 2011 at 7:00
  • I am not talking about a cmdlet. Of course you can use a cmdlet ( what is powershell without cmdlets!!) I am talking about a manual intervention, where a dialog box appears, you then don't enter password and return back. That is not a practical solution. Everytime a script runs, you expect to get that dialog and return back?
    – manojlds
    Commented Jul 27, 2011 at 7:02
  • 2
    I suggest you read the question again. There's nothing in it with regard to manual intervention.
    – Shay Levy
    Commented Jul 27, 2011 at 7:50
  • @Shay Levy - I'm avoid the credential prompt. But yes, I should have explicitly specified that Commented Jul 27, 2011 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.