14

I used ls alias: and tried to find &. However, & is not in the output. What's &? Is it the combination of Invoke-Command and Invoke-Expression?

Are there any other operators which don't have a cmdlet in PowerShell?

5
  • 1
    It's an operator, thus a part of the language syntax like +.
    – wOxxOm
    Feb 22, 2017 at 5:33
  • 2
    Operators != aliases. Lachie White has answered about &. As for the rest of them, there are too many to list. Read the help-files listed here Get-Help *operators*
    – Frode F.
    Feb 22, 2017 at 7:13
  • @FrodeF. executing Get-Help *operators* showed nothing.
    – default
    Feb 22, 2017 at 9:07
  • 2
    The asterisks were by design. You can wildcard search with get-help for topics. At least in newer PS versions. Try update-help to make sure you have downloaded all help files
    – Frode F.
    Feb 22, 2017 at 10:33
  • Testet wildcard search in PS2.0 now and it works (at least with PowerShell.exe -version 2), so it probably returned nothing because you hadn't downloaded the help-files with update-help. :-)
    – Frode F.
    Feb 22, 2017 at 11:41

2 Answers 2

19

The call operator & allows you to execute a command, script or function.

Many times you can execute a command by just typing its name, but this will only run if the command is in the environment path. Also if the command (or the path) contains a space then this will fail. Surrounding a command with quotes will make PowerShell treat it as a string, so in addition to quotes, use the & call operator to force PowerShell to treat the string as a command to be executed.

PowerShell Call Operator

I am not sure of all the operators that are in PowerShell, but another really useful one is --%, used to stop parsing.

The stop-parsing symbol --%, introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting any further input on the line as PowerShell commands or expressions.

PowerShell Stop Parsing Operator

Building on the comments that have been made:

Get-Help About_Operators

It will show you the best overview of all operator types and abstract ones like the Call and Stop Parsing Operators.

2
4

Some other uses for the call operator, not well documented:

# Get and set a variable in a module scope with the call operator 
# get module object
$m = get-module counter

& $m Get-Variable count
& $m Set-Variable count 33

# see module function definition
& $m Get-Item function:Get-Count

# run a cmdlet with a commandinfo object and the call operator
$d = get-command get-date
& $d

You can think of & { } as an anonymous function.

1..5 | & { process{$_ * 2} }

Another really useful operator is the subexpression operator. $( ) It's not just for inside strings. You can combine two statements and make them act as one.

$(echo hi; echo there) | measure

If and Foreach statements can go inside them too. You couldn't normally pipe from foreach (). So anywhere you could put an expression or pipeline, with $() you can put any statement or group of statements.

$(foreach ($i in 1..10) { $i;sleep 1 } ) | Out-Gridview

Although, I like streaming from foreach with the call operator (or function), so I don't have to wait.

 & {foreach ($i in 1..10) { $i;sleep 1 } } | Out-GridView
5
  • 1
    I didn't know there were these kinds of useful behaviors. Thank you for sharing your knowledge. Have a good day. Jun 28, 2021 at 19:36
  • 1
    @SmartHumanism Thanks. I got it from the book Windows Powershell In Action.
    – js2010
    Jun 29, 2021 at 1:35
  • Thank you for your politeness and for sharing the source you learned from. If I may ask you a bit, & {foreach ($i in 1..10) { $i;sleep 1 } } seems to work the same way % {foreach ($i in 1..10) { $i;sleep 1 } }. What would be the difference between the & operator and the % operator, here? Jun 29, 2021 at 8:05
  • @SmartHumanism They don't really compare. % is foreach-object, which you can pipe from compared to the other foreach ().
    – js2010
    Jun 29, 2021 at 13:08
  • I tested and it seems that % can be used in a similar way as &. The difference seems to be the scope, if you run & {$temp = 3} then, when you type $temp there shows no outcome, whereas, in the case of % {$temp = 3} you can get $temp's value in the current PowerShell scope. Jul 1, 2021 at 16:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.