39

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.

Community
  • 1
  • 1
LostInComputer
  • 15,188
  • 4
  • 41
  • 49

2 Answers2

58

Solution:

$mycreds = New-Object System.Management.Automation.PSCredential 
              ("username", (new-object System.Security.SecureString))
SteveC
  • 15,808
  • 23
  • 102
  • 173
LostInComputer
  • 15,188
  • 4
  • 41
  • 49
  • hi @EugeneO hope u can assist me with this http://stackoverflow.com/questions/9290830/powershell-secure-string-conversion tks – JackyBoi Feb 15 '12 at 09:32
  • 4
    PowerShell is too convoluted for there to be such a thing as a dumb question. – jpmc26 Mar 30 '17 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 Sep 07 '17 at 13:42
  • this is a bad idea – Golden Lion Feb 24 '22 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
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 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 Jul 27 '11 at 06: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 Jul 27 '11 at 07: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 Jul 27 '11 at 07:02
  • 2
    I suggest you read the question again. There's nothing in it with regard to manual intervention. – Shay Levy Jul 27 '11 at 07:50
  • @Shay Levy - I'm avoid the credential prompt. But yes, I should have explicitly specified that – LostInComputer Jul 27 '11 at 14:00