Windows Dev Center

The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ValidateLengthAttribute Constructor

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

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

Dim minLength As Integer
Dim maxLength As Integer

Dim instance As New ValidateLengthAttribute(minLength, maxLength)

public ValidateLengthAttribute (
	int minLength,
	int maxLength
public ValidateLengthAttribute (
	int minLength, 
	int maxLength
public function ValidateLengthAttribute (
	minLength : int, 
	maxLength : int



The minimum length that is allowed for the cmdlet parameter argument.


The maximum length that is allowed for the cmdlet parameter argument.

The following lines of code show how this attribute may 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);

Exception typeCondition

minLength or maxLength has an argument that is not valid.


maxLength is less than minLength.

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 ValidateLengthAttribute Declaration.

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


Target Platforms

© 2015 Microsoft