PowerShell报错‘找不到接受参数的位置参数’求助
Let's break down what's causing the error and how to fix it quickly.
The Root Cause
Your original line copy-item $_.source\$_.file $_.destination\$_.file is tripping up PowerShell because of how it parses path expressions. When you write $_.source\$_.file without proper quoting or path construction, PowerShell treats $_.source as the first positional argument to Copy-Item (the -Path parameter), then tries to interpret \$_.file as a second positional argument—but Copy-Item doesn't have a parameter that accepts that extra input, hence the error.
The Solutions
There are two reliable ways to construct your source and destination paths correctly:
1. Use String Interpolation (Double Quotes)
Wrap your path expressions in double quotes, and use subexpressions $() to properly resolve the object properties within the string:
Import-Csv sample1.csv | ForEach-Object { # Construct full source and destination paths $sourceFullPath = "$($_.source)\$($_.file)" $destFullPath = "$($_.destination)\$($_.file)" # Execute the copy operation Copy-Item -Path $sourceFullPath -Destination $destFullPath }
The $() syntax ensures PowerShell evaluates the $_.source and $_.file properties correctly before combining them into a single string path.
2. Use Join-Path (Recommended for Robust Path Handling)
The Join-Path cmdlet is designed specifically to combine path segments, and it automatically handles issues like trailing/leading slashes or spaces in paths:
Import-Csv sample1.csv | ForEach-Object { # Build paths using Join-Path for reliability $sourceFullPath = Join-Path -Path $_.source -ChildPath $_.file $destFullPath = Join-Path -Path $_.destination -ChildPath $_.file Copy-Item -Path $sourceFullPath -Destination $destFullPath }
This method is safer, especially if your CSV contains paths with spaces or inconsistent path separators.
Quick Notes
- If you need to move files instead of copying, just replace
Copy-ItemwithMove-Item. - Double-check that the paths in your CSV are valid (e.g., no typos, source paths exist) to avoid additional errors.
内容的提问来源于stack exchange,提问作者Mohammed Zubairu

