How to prompt for credentials just once

Usually when I open a shell at the beginning of the day it stays open the whole day. During that time I may run SQL queries, web requests, you name it. I don’t mind entering credentials for these (verses storing them in a text file), but it is nice when I only have to enter them once.

Enter a short function called ‘creds’. Creds will look for a variable named $creds and if it doesn’t find one it will prompt me for a password.


function creds {
    if( ! $creds ) {
     $script:creds = get-credential -Message "webSvc creds" -UserName ""

When I run a command that uses the creds function it will prompt me when it can’t find them. In this example I’m calling a web service.

function new-webSvcQuery {
param( $Query )
    $creds = creds
    try {
        $webSvr = ""
        $webSvrPxy = New-WebServiceProxy -Uri $webSvr -credential $creds
        $webSvrData = ( $webSvrPxy.AsXml($Query) ).Data1
    catch [System.Net.WebException] {
     throw "your query is broke man"
    finally {

You can adapt the idea to any interface that takes a PS Credential object


This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s