Bird
0
0

You want to automate a task on macOS that creates a directory only if it does not exist. Which PowerShell snippet correctly does this?

hard📝 Application Q9 of 15
PowerShell - Cross-Platform PowerShell
You want to automate a task on macOS that creates a directory only if it does not exist. Which PowerShell snippet correctly does this?
Amkdir ./NewFolder
Bif (-not (Test-Path -Path './NewFolder')) { New-Item -ItemType Directory -Path './NewFolder' }
CNew-Item -ItemType Directory -Path './NewFolder'
Dif (Test-Path -Path './NewFolder') { Remove-Item './NewFolder' }
Step-by-Step Solution
Solution:
  1. Step 1: Check existence with Test-Path

    Test-Path checks if directory exists; -not negates to create only if missing.
  2. Step 2: Create directory conditionally

    New-Item with -ItemType Directory creates the folder only if it doesn't exist.
  3. Final Answer:

    if (-not (Test-Path -Path './NewFolder')) { New-Item -ItemType Directory -Path './NewFolder' } -> Option B
  4. Quick Check:

    Use Test-Path to conditionally create directories [OK]
Quick Trick: Use Test-Path to check before creating folders [OK]
Common Mistakes:
  • Using mkdir without existence check
  • Using New-Item without existence check
  • Removing folder instead of creating

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes