Create PSCredential without a password

LostInComputer picture LostInComputer · Jul 27, 2011 · Viewed 13k times · Source

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.

Answer

LostInComputer picture LostInComputer · Jul 27, 2011

Solution:

$mycreds = New-Object System.Management.Automation.PSCredential 
              ("username", (new-object System.Security.SecureString))