Using The PowerShell ValidateLength Parameter Attribute

What do you think a parameter validation attribute in PowerShell called ValidateLength does anyway? Josh does and he’s going to school you on it in this video. He’s able to restrict input to a function parameter by limiting its string length to a certain value. Next thing you’re going to tell me is that he’s not really a king too, right?

And this example function. I'm using the validate length attribute within square brackets to define what strings my computer name parameter who consider valid within the parentheses. Of defined that any string must be at least 3 characters long and cannot exceed 15 characters in length. When I run this function with the computer name that's only 2 characters long, I get the error message that you would expect telling me that my argument is too short and what I need to expand it to be valid. If I run it again with the computer name that's 3 characters, the function runs. And if I run the function with a long computer name. Again, I get the error message would expect telling me that my argument was too long and what I need to shorten it too. It is possible to use the validate length attribute outside of a function directly against a variable to do that define the validate length attribute again within square brackets and within parentheses. Your minimum and maximum character lengths for the value and then you can assign something to that variable in this case. My first string has a minimum length of one in a maximum length of 2 and if I run this example. It's too long. Notice that the error message isn't as user friendly as when you do it within a function. My second example here has a minimum length of 3 a maximum length of 15, but this time, we're reading values from the end user using read host when I run. Thus, I am prompted for a target name and if I supply a short computer name. I'm told that some.