Skip to content

about_Pwsh / about_PowerShell.exe suggest use of a script block with the call operator inside command strings, even though that seems unnecessary #2750

@mklement0

Description

@mklement0

Said topics currently state:

To write a string that runs a PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

However, there is no good reason to use & with a script block { ... } inside a string passed to -Command- it works fine - and more efficiently - without it; e.g., using the command from the example (which should be modified too)

Instead of:

pwsh -command '& { Get-Command -Name Get-Command }'

you can simply use:

pwsh -command 'Get-Command -Name Get-Command'

So, clearly, the use of embedded & { ... } is not a general requirement.

If there are special circumstances where it is required, they should be mentioned.


Also: The formatting of the above code is currently broken in the about_powershell_exe documents (but not in about_Pwsh)


As an aside: I find the following confusing:

If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.

Perhaps it should say something like:

"
If the value of Command is a string, Command must be the last parameter in the command, because all arguments following it are interpreted as part of the command to execute.
"

Version(s) of document impacted

  • Impacts 6.1 document
  • Impacts 6.0 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Metadata

Metadata

Assignees

Labels

Pri2Priority - Medium

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions