You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell报错‘找不到接受参数的位置参数’求助

Fixing "A positional parameter cannot be found that accepts argument" in your PowerShell Copy-Item script

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-Item with Move-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:08