Mandatory Password Parameters

by Feb 7, 2011

If you mark a parameter as mandatory and set its type to "SecureString," PowerShell will automatically prompt for the password with masked characters:

function Test-Password {
param(
[System.Security.SecureString][Parameter(Mandatory=$true)]$password
)
$plain = (New-Object System.Management.AUtomation.PSCredential('dummy',$password)).`
GetNetworkCredential().password
"You entered: $plain"
}

Twitter This Tip!
ReTweet this Tip!