How to validate credential argument for your function

You may know that it’s a best practice to validate arguments you pass to your function.

For example, if you want to validate that your parameter named MyString is not null nor empty you can use the ValidateNotNullOrEmpty attribute.

function Foo
{
    Param([ValidateNotNullOrEmpty()]$MyString)

    #Doing some stuff
}

But there are other validations which must be done through casting1 or type accelerators. And fortunately, there is data type dedicated to credentials, which is simply named PSCredential.

For example:

function Foo
{
    Param([PSCredential]$Credential)

    #Doing some stuff
}

More about


  1. Casting refers to the process of changing a variable’s data type to another data type. 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s