142

It seems that the % operation starts script blocks after the pipeline, although about_Script_Blocks indicates the % isn't necessary.

These all work just fine.

get-childitem | % { write-host $_.Name }

{ write-host 'hello' }

% { write-host 'hello' }

But when we add a script block after the pipeline, we need to have the % first.

get-childitem | { write-host $_.Name }
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467

3 Answers3

194

When used in the context of a cmdlet (such as your example), it's an alias for ForEach-Object:

> Get-Alias -Definition ForEach-Object

CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           %                                                   ForEach-Object
Alias           foreach                                             ForEach-Object

When used in the context of an equation, it's the modulus operator:

> 11 % 5

1

and as the modulus operator, % can also be used in an assignment operator (%=):

> $this = 11
> $this %= 5
> $this

1
Kohlbrr
  • 3,861
  • 1
  • 21
  • 24
  • 2
    I will mark this as answer once it also contains the % role as a modulus operator in different contexts. – Shaun Luttin Apr 03 '14 at 19:09
  • 1
    Added equation and assignment operator examples – Kohlbrr Apr 03 '14 at 19:41
  • 23
    In the case of Foreach-Object, I'd advise not using the "%" symbol - it makes it harder for folks to read/maintain your code - especially people new to PS. Let's face it, the more people that pick up PS, the better. – Simon Catlin Apr 03 '14 at 20:17
  • 1
    Where can I find the ForEach-Object meaning in the official documentation on MSDN? –  Oct 24 '19 at 18:55
  • I disagree Simon Catlin, by that logic we should all be using assembly because the rest is just syntactic sugar – reggaeguitar Feb 06 '20 at 21:36
  • Another use of the percent symbol can't be simply replaced by ForEach-Object: Get-ChildItem | % Name – robbie fan Dec 16 '20 at 13:37
23

A post PowerShell - Special Characters And Tokens provides description of multiple symbols including %

% (percentage)

1. Shortcut to foreach.
Task: Print all items in a collection.
Solution.
... | % { Write-Host $_ }

2. Remainder of division, same as Mod in VB.
Example:
5 % 2
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
  • 7
    @ShaunLuttin: The value is a link, which describes multiple special characters including % – Michael Freidgeim Dec 30 '16 at 00:02
  • Technically it's an alias for `ForEach-Object`. `ForEach` is also an alias for `ForEach-Object` - except when used at the start of a statement, where it's a loop control keyword (like `while`). – mwfearnley Aug 31 '18 at 09:15
7

% can replace Get-ChildItem | ForEach-Object { write-host $_.Name } which will not work without either the % or the ForEach-Object.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Xao
  • 512
  • 6
  • 17