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

4
  • 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... Commented Jun 29, 2023 at 12:31
  • 1
    See also: stackoverflow.com/a/49130611/7571258 Commented Jun 29, 2023 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. Commented Jun 29, 2023 at 14:27
  • You should be able to simply invoke the file not dissimilar to a function block using & if by command line you mean generically, but the dupe link for #56551242 is what you want for cmd Commented Jun 29, 2023 at 14:31

1 Answer 1

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 }"
Sign up to request clarification or add additional context in comments.

1 Comment

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 erroneously suggested that & { ... } is required, but this has since been corrected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.