Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 1.53 KB

File metadata and controls

42 lines (31 loc) · 1.53 KB
description How to validate an argument using a script
ms.date 06/11/2024
title How to validate an argument using a script

How to validate an argument using a script

This example shows how to specify a validation rule that uses a script to check the parameter argument before the cmdlet is run. The value of the parameter is piped to the script. The script must return $true for every value piped to it.

Note

For more information about the class that defines this attribute, see System.Management.Automation.ValidateScriptAttribute.

To validate an argument using a script

  • Add the ValidateScript attribute as shown in the following code. This example specifies a script to validate that the input value is an odd number.

    [ValidateScript("$_ % 2", ErrorMessage = "The item '{0}' did not pass validation of script '{1}'")]
    [Parameter(Position = 0, Mandatory = true)]
    public int32 OddNumber
    {
      get { return oddNumber; }
      set { oddNumber = value; }
    }
    
    private int32 oddNumber;

For more information about how to declare this attribute, see ValidateScript Attribute Declaration.

See Also

System.Management.Automation.ValidateScriptAttribute

ValidateScript Attribute Declaration

Writing a Windows PowerShell Cmdlet