Bird
0
0

You want to create a new folder named Reports inside C:\Data only if it does not already exist. Which command achieves this?

hard📝 Application Q8 of 15
PowerShell - File and Directory Operations
You want to create a new folder named Reports inside C:\Data only if it does not already exist. Which command achieves this?
ARemove-Item 'C:\Data\Reports'; New-Item -Path 'C:\Data\Reports' -ItemType Directory
BNew-Item -Path 'C:\Data\Reports' -ItemType Directory -Force
CNew-Item -Path 'C:\Data' -Name 'Reports' -ItemType File
Dif (-not (Test-Path 'C:\Data\Reports')) { New-Item -Path 'C:\Data\Reports' -ItemType Directory }
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional folder creation

    Use Test-Path to check if folder exists before creating it.
  2. Step 2: Analyze each option

    if (-not (Test-Path 'C:\Data\Reports')) { New-Item -Path 'C:\Data\Reports' -ItemType Directory } checks existence and creates folder only if missing. New-Item -Path 'C:\Data\Reports' -ItemType Directory -Force forces creation but overwrites. New-Item -Path 'C:\Data' -Name 'Reports' -ItemType File creates a file, not folder. Remove-Item 'C:\Data\Reports'; New-Item -Path 'C:\Data\Reports' -ItemType Directory deletes then creates, which is unsafe.
  3. Final Answer:

    if (-not (Test-Path 'C:\Data\Reports')) { New-Item -Path 'C:\Data\Reports' -ItemType Directory } -> Option D
  4. Quick Check:

    Use Test-Path to avoid overwriting [OK]
Quick Trick: Use Test-Path before New-Item to avoid duplicates [OK]
Common Mistakes:
  • Using -Force to overwrite without check
  • Creating file instead of directory
  • Deleting folder before creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes