1

I try to run a script

powershell -File somescript.ps1 -Arg1 $false -Arg2 $false -Arg3 $false

I keep getting

A positional parameter cannot be found that accepts argument '$false'.
  • I really want to see each parameters name, not just pass values without knowing what it is
  • there are more parameters availble in the script with default values so I dont need to specify them

any help appreciated

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    If you look inside "somescript.ps1", is ```Arg1``` a ```[bool]``` or ```[switch]``` parameter? If it's a switch then PowerShell is treating ```-Arg1``` in your command as equivalent to ```-Arg1:$true``` (switch arguments don't take a separate value - the parameter name on its own assumes ```$true```, or you separate with a colon rather than a space - e.g. ```-Arg:$false```) so you've then got a dangling ```$false``` straight after the space that powershell doesn't know what to do with, which is probably what the error is trying to tell you... – mclayton Jun 29 '23 at 12:31
  • 1
    See also: https://stackoverflow.com/a/49130611/7571258 – zett42 Jun 29 '23 at 12:47
  • Due to a _design limitation_ in _Windows PowerShell_, Boolean values cannot be passed via `powershell.exe`'s `-File` parameter. The _workaround_ - which is no longer necessary in _PowerShell (Core) 7+_ (`pwsh.exe`) - is to use the `-Command` (`-c`) parameter instead (which fundamentally changes the syntax rules). However, if the target script uses `[switch]` parameters, which is preferable, passing Boolean values _explicitly_ is normally not needed. See the linked duplicate for details. – mklement0 Jun 29 '23 at 14:27
  • You should be able to simply invoke the file not dissimilar to a function block [using `&`](https://i.stack.imgur.com/9R7Gn.png) if by command line you mean generically, but the dupe link for #56551242 is what you want for *cmd* – Hashbrown Jun 29 '23 at 14:31

1 Answers1

1

It is all a question of syntax, and one MUST use the -Command parameter like so:

powershell -Command "& { somescript.ps1 -Arg1:$false -Arg1:$false -Arg3:$false }"
  • That works, but note that there's no reason to use `"& { ... }"` in order to invoke code passed to PowerShell's CLI via the `-Command` (`-c`) parameter - just use `"..."` directly. Older versions of the [CLI documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh) erroneously suggested that `& { ... }` is required, but this has since been corrected. – mklement0 Jun 29 '23 at 14:29