Bird
0
0

You wrote this script to check if a folder exists:

medium📝 Debug Q14 of 15
PowerShell - File and Directory Operations
You wrote this script to check if a folder exists:
if (Test-Path -Path 'D:\Data') {
  Write-Host 'Folder found'
} else {
  Write-Host 'Folder missing'
}

But it always prints 'Folder missing' even when the folder exists. What is the likely problem?
APowerShell requires double backslashes in paths
BTest-Path does not accept the -Path parameter
CThe folder path is incorrect or misspelled
DWrite-Host cannot print strings
Step-by-Step Solution
Solution:
  1. Step 1: Check Test-Path syntax

    Test-Path accepts the -Path parameter correctly, so syntax is fine.
  2. Step 2: Consider common causes for false result

    If it always prints 'Folder missing', the folder path is likely wrong or misspelled, causing Test-Path to return False.
  3. Final Answer:

    The folder path is incorrect or misspelled -> Option C
  4. Quick Check:

    Wrong path causes Test-Path False = D [OK]
Quick Trick: Check folder path spelling if Test-Path always returns False [OK]
Common Mistakes:
  • Thinking -Path is invalid parameter
  • Assuming Write-Host can't print
  • Believing backslashes need doubling in single quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes