ValidateLengthAttribute Constructor (Int32, Int32)


Updated: April 27, 2016

Applies To: Windows PowerShell

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)

public ValidateLengthAttribute(
	int minLength,
	int maxLength


Type: System.Int32

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

Type: System.Int32

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

Exception Condition

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 ValidateLength Attribute Declaration.

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

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);

Return to top

Community Additions