Po$H Pete | Those who can… $cript
24Jan/110

Creating a PS Credential Object from Plain Text

Storing passwords in plain text isn't generally a great idea. However, sometimes you need to do it due to a lack of any other options. The following function is one that I wrote which allows you to pass in a username and password as plain text and it'll return a pscredential object that you can use to pass into other functions such as the -Credential parameter on a Get-WMI query etc.

Function Get-PSCredential($User,$Password)
{
	$SecPass = convertto-securestring -asplaintext -string $Password -force
	$Creds = new-object System.Management.Automation.PSCredential -argumentlist $User,$SecPass
	Return $Creds
}

To call this function and return the credential object into a variable, just do something like this:

$MyCreds = Get-PSCredential -User "MyUserName" -Password "MyPassword"

Your credential object is then built in the $MyCreds variable.