Skip to content

/errors/powershell/a-positional-parameter-cannot-be-found

PowerShell: A positional parameter cannot be found that accepts argument 'x'.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
PowerShell
exit code
1
versions
Windows PowerShell 5.1, PowerShell 7.x
os
windows, linux, macos

A positional parameter cannot be found that accepts argument 'Files'. means the cmdlet received an argument without a parameter name, and all of its positional slots were already taken. The quoted word is the first argument that did not fit. Most often it is the second half of an unquoted path: Set-Location C:\Program Files passes C:\Program and Files as two arguments. Quote the value.

Reproduce it

powershell
$ErrorActionPreference = 'Stop'
'x' | Set-Content a.txt
foreach ($sb in { Set-Location C:\Program Files }, { Copy-Item a.txt b.txt c.txt }) {
  try { & $sb } catch { "$sb`n  -> $($_.Exception.Message)" }
}
Copy-Item -Path a.txt -Destination 'b c.txt'
Get-ChildItem -Name *.txt

Output

 Set-Location C:\Program Files
  -> A positional parameter cannot be found that accepts argument 'Files'.
 Copy-Item a.txt b.txt c.txt
  -> A positional parameter cannot be found that accepts argument 'c.txt'.
a.txt
b c.txt

Copy-Item has two positional parameters (-Path and -Destination), so the third value has nowhere to go.

How many positional parameters does a cmdlet have?

Get-Command -Syntax shows them: parameters in [[-Name] <type>] form (name in its own brackets) are positional.

powershell
((Get-Command Copy-Item -Syntax).Trim() -split '\r?\n')[0]
(Get-Command Copy-Item).ParameterSets[0].Parameters |
  Where-Object Position -ge 0 | Sort-Object Position |
  ForEach-Object { "position $($_.Position): -$($_.Name)" }

Output

Copy-Item [-Path] <string[]> [[-Destination] <string>] [-Container] [-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-FromSession <PSSession>] [-ToSession <PSSession>] [<CommonParameters>]
position 0: -Path
position 1: -Destination

Fixes by cause

WrongRight
Set-Location C:\Program FilesSet-Location 'C:\Program Files'
Get-Process -Name chrome notepadGet-Process -Name chrome, notepad
Copy-Item a.txt b.txt c.txt dest\Copy-Item a.txt, b.txt, c.txt -Destination dest\

Sources