#requires -version 2.0

Function New-PSCredential {

<#
.Synopsis
Create a PSCredential
.Description
Build a PSCredential object from a username and password. The username must be
in the format domain\username. If your password includes a $ be sure to enclose it
in single quotes. If you don't specify a password, you will be prompted.
.Parameter Username
The user account name in the format domain\username
.Parameter Password
A plain string password. Enclose passwords with $ characters in single quotes.
.Example
PS C:\> $cred=new-pscredential "mycompany\jeff" 'Pa$$w0rd+'
#>

Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a username in the format domain\username")]
[ValidatePattern({^\w*\\\w*$})]
[string]$Username,
[Parameter(Position=1)]
[string]$Password

)

if ($Password) {
    $securepass=ConvertTo-SecureString -String $Password -AsPlainText -Force
}
else {
    $securepass=Read-Host "Enter a password for $username" -AsSecureString
}

New-Object System.Management.Automation.PSCredential $username,$securepass

} #end function