PowerShell: Using PSCredentials Without a Prompt


You cannot use get-credential without some type of prompt (although you can do it without the pop-up dialog), however you can save your securestring password to a file, reload it for later, and manually create a credential without a prompt. Of course the problem with this is that your password will be exposed to anyone with access to the file, so do this at your own risk.

First, choose your password and write it to a file:

PS C:\> read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt

In the future, you won’t have to enter your credentials over and over again, instead you can just read in your password from the file, and create a new PSCredential object from that. Then you can use that credential to perform various tasks like connecting to ftp servers and such, like so:

PS C:\> $pass = cat C:\securestring.txt | convertto-securestring
PS C:\> $mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "test",$pass
PS C:\> get-ftp -server -cred $mycred -list *.vb

DirEntry : -rw------- 1 1036 100 1044 Dec 07 17:39 AssemblyInfo.vb
FileName : AssemblyInfo.vb
FileSize : 1044
FileTime : Dec 07 17:39
IsDir : False

Leave a Reply

Your email address will not be published. Required fields are marked *