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 10.0.1.1 -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

PS C:\> get-bufferhtml | out-file sample.html

Technorati : NetCmdlets, PowerShell, cmdlet, get-credential

This article is part of the GWB Archives. Original Author: Lance Robinson

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.