/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
$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 *.txtOutput
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.
((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
| Wrong | Right |
|---|---|
| Set-Location C:\Program Files | Set-Location 'C:\Program Files' |
| Get-Process -Name chrome notepad | Get-Process -Name chrome, notepad |
| Copy-Item a.txt b.txt c.txt dest\ | Copy-Item a.txt, b.txt, c.txt -Destination dest\ |
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules
Related pages
errors · powershellPowerShell: Cannot bind argument to parameter 'Path' because it is null.A variable passed to a mandatory parameter is $null or empty: a typo in its name, a lookup that found nothing, or a script parameter that was never passed.glossary · powershellWhat is splatting in PowerShell? (@params instead of $params)Splatting passes a hashtable of parameter names and values to a command as @name instead of $name. It replaces long lines and lets you add options in code.cheat sheets · powershellPowerShell cheat sheet: files, objects, filtering, strings and error handlingPowerShell cheat sheet: find commands and help, work with files, filter and sort objects, format strings, hashtables, loops, errors and bash equivalents.