Bird
0
0

How can you create multiple empty files named file1.txt, file2.txt, and file3.txt in C:\Docs using a single PowerShell command?

hard📝 Application Q9 of 15
PowerShell - File and Directory Operations
How can you create multiple empty files named file1.txt, file2.txt, and file3.txt in C:\Docs using a single PowerShell command?
A1..3 | ForEach-Object { New-Item -Path "C:\Docs\file$_.txt" -ItemType File }
BNew-Item -Path 'C:\Docs\file1.txt','C:\Docs\file2.txt','C:\Docs\file3.txt' -ItemType File
CNew-Item -Path 'C:\Docs' -Name 'file1.txt','file2.txt','file3.txt' -ItemType File
DNew-Item -Path 'C:\Docs\file*.txt' -ItemType File
Step-by-Step Solution
Solution:
  1. Step 1: Understand looping for multiple files

    PowerShell can loop with 1..3 and create files dynamically.
  2. Step 2: Analyze options

    1..3 | ForEach-Object { New-Item -Path "C:\Docs\file$_.txt" -ItemType File } uses a loop to create each file with correct names. New-Item -Path 'C:\Docs\file1.txt','C:\Docs\file2.txt','C:\Docs\file3.txt' -ItemType File tries to create multiple files in one command but New-Item does not accept array paths. New-Item -Path 'C:\Docs' -Name 'file1.txt','file2.txt','file3.txt' -ItemType File misuses -Name with multiple values. New-Item -Path 'C:\Docs\file*.txt' -ItemType File uses wildcard which does not create files.
  3. Final Answer:

    1..3 | ForEach-Object { New-Item -Path "C:\Docs\file$_.txt" -ItemType File } -> Option A
  4. Quick Check:

    Use loop with New-Item for multiple files [OK]
Quick Trick: Use loops to create multiple files efficiently [OK]
Common Mistakes:
  • Passing arrays directly to -Path
  • Using wildcards to create files
  • Misusing -Name with multiple values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes