ValidateSet with variable

How can I make the “ValidateSet” to work with $cred from the BEGIN section ?

I know that [ValidateSet(‘domain1\user2’, ‘domain1\user1’,"$cred")] will not work .

function Get-folderssize {


[Parameter(Mandatory = $True)]
[Alias(‘CN’, ‘MachineName’, ‘Name’)]

[Parameter(Mandatory = $True)]
[ValidateSet(‘domain1\user2’, ‘domain1\user1’)]

[Parameter(Mandatory = $True)]



$encrypted = “01000000”
$user = “domain1\user1”
$password = ConvertTo-SecureString -string $encrypted
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $password

Hi Arik,

You can use ValidateScript instead of ValidateSet.

Please refer to this link.

Thank you.

It’s not a best practice to manage permissions with a function wrapper, the permissions should be managed on the endpoint where you accessing folder permissions. To provide an example, you would need to do a ValidateScript to parse out the username from the Credential object.

function Test-It {


        [Parameter(Mandatory = $True)]
            if ('MyUserName','YourUserName' -contains $_.UserName) {
            } else {
                Throw [System.UnauthorizedAccessException] ("The user {0} is not authorized." -f $_.UserName)

    process{'Processing user {0}' -f $Credential.UserName}

Test-It -Credential $creds