ValidateLengthAttribute Class


Updated: April 27, 2016

Applies To: Windows PowerShell

Defines an attribute that uses minimum and maximum limits to validate the number of characters in a cmdlet parameter argument. (This attribute can also be used by Windows PowerShell functions.) This class cannot be inherited.

Namespace:   System.Management.Automation
Assembly:  System.Management.Automation (in System.Management.Automation.dll)

[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class ValidateLengthAttribute : ValidateEnumeratedArgumentsAttribute

System_CAPS_pubmethodValidateLengthAttribute(Int32, Int32)

Initializes a new instance of the ValidateLengthAttribute class with the minimum and maximum lengths for the cmdlet parameter arguments.


Gets the maximum number of characters (length) that is allowed for the cmdlet parameter argument.


Gets the minimum number of characters (length) that is allowed for the cmdlet parameter argument.


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Object.)


(Inherited from Attribute.)


(Inherited from Attribute.)


(Inherited from Object.)

There are no established scenarios for explicitly creating a ValidateLengthAttribute object from within a cmdlet. The ValidateLengthAttribute object is created and used indirectly when the ValidateLength attribute is declared.

This attribute is declared when implementing a cmdlet parameter. For more information about the syntax used to declare this attribute, see ValidateLength Attribute Declaration.

For examples of declaring other validation attributes, see How to Declare Input Validation Rules.

The following code example shows how this attribute can be declared.

[Cmdlet(VerbsDiagnostic.Test, "ArgumentLength")]
public class TestArgumentLengthCommand : Cmdlet
  [ValidateLength(0, 10)]
  [Parameter(Position = 0, Mandatory = true)]
  public string UserName
    get { return userName; }
    set { userName = value; }
  private string userName;

  // Override input processing method.
  protected override void ProcessRecord()
    WriteObject("Hello " + userName);

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top