Bird
0
0

You want to move all .txt files from C:\Source to D:\Destination, but only if they are not already present there. Which script snippet achieves this?

hard📝 Application Q8 of 15
PowerShell - File and Directory Operations
You want to move all .txt files from C:\Source to D:\Destination, but only if they are not already present there. Which script snippet achieves this?
AGet-ChildItem -Path 'C:\Source' -Filter '*.txt' | Where-Object { -not (Test-Path -Path (Join-Path 'D:\Destination' $_.Name)) } | Move-Item -Destination 'D:\Destination'
BMove-Item -Path 'C:\Source\*.txt' -Destination 'D:\Destination' -Force
CCopy-Item -Path 'C:\Source\*.txt' -Destination 'D:\Destination' -Exclude '*.txt'
DGet-ChildItem -Path 'C:\Source' | Move-Item -Destination 'D:\Destination'"
Step-by-Step Solution
Solution:
  1. Step 1: Filter .txt files and check existence

    Get-ChildItem lists .txt files; Where-Object filters those not existing in destination using Test-Path.
  2. Step 2: Move only filtered files

    Pipe filtered files to Move-Item with destination path to move only missing files.
  3. Final Answer:

    Get-ChildItem -Path 'C:\Source' -Filter '*.txt' | Where-Object { -not (Test-Path -Path (Join-Path 'D:\Destination' $_.Name)) } | Move-Item -Destination 'D:\Destination' -> Option A
  4. Quick Check:

    Filter missing files before Move-Item to avoid overwrites [OK]
Quick Trick: Use Test-Path to check destination before moving files [OK]
Common Mistakes:
  • Using Move-Item with -Force blindly overwrites files
  • Using Copy-Item instead of Move-Item
  • Not filtering files before moving

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes