Sometimes, while getting user input, you may want it to restrict it to certain characters. This may be for various reasons depending upon the situation. While there is no direct cmdlet or function to do this, you can use regex to help you to determine if the input matches certain pattern. For example, let’s consider that we want to restrict integer length input by user to 6 characters. for this, we can use below regex:
^\d{6}$
In action:
- Below example is for a valid case, where input is of 6 characters only:

- Below example is for a invalid case, where input is less than 6 characters:

3. Below example is for a invalid case, where one of the input characters is not a integer:

We can also use something like:
‘^\d{2,6}$’
to validate that the int is between two and six digits long.
#3 is missing the \ for anyone looking for some help! Great article though!!
LikeLike