Bird
0
0

You have a parameter $FilePath that must be a non-empty string and match a pattern ending with '.txt'. How do you combine parameter validation attributes to enforce this?

hard📝 Application Q9 of 15
PowerShell - Scripting Best Practices
You have a parameter $FilePath that must be a non-empty string and match a pattern ending with '.txt'. How do you combine parameter validation attributes to enforce this?
A[ValidateLength(11,100)][ValidatePattern('.*\.txt$')] [string]$FilePath
B[ValidateNotNullOrEmpty()][ValidatePattern('.*\.txt$')] [string]$FilePath
C[ValidateSet('*.txt')][ValidateNotNullOrEmpty()] [string]$FilePath
D[ValidateRange(1,100)][ValidatePattern('.*\.txt$')] [string]$FilePath
Step-by-Step Solution
Solution:
  1. Step 1: Ensure parameter is not empty

    [ValidateNotNullOrEmpty()] prevents null or empty strings.
  2. Step 2: Use regex to match '.txt' extension

    [ValidatePattern('.*\.txt$')] enforces the string ends with '.txt'.
  3. Final Answer:

    [ValidateNotNullOrEmpty()][ValidatePattern('.*\.txt$')] [string]$FilePath -> Option B
  4. Quick Check:

    Combine NotNullOrEmpty and Pattern for non-empty matching strings [OK]
Quick Trick: Use ValidatePattern with regex and ValidateNotNullOrEmpty together [OK]
Common Mistakes:
  • Using ValidateSet for file extensions
  • Applying ValidateRange to strings
  • Missing NotNullOrEmpty check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes