% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.
You can use one or more arithmetic operators to add, subtract, multiply, and divide values, and to calculate the remainder (modulus) of a division operation. In addition, the addition operator ( + ) and multiplication operator ( * ) also operate on strings, arrays, and hash tables.
Syntax Description You can type a comment symbol ( # ) before each line of comments, or you can use the <# and #> symbols to create a comment block. All the lines within the comment block are interpreted as comments. Each section of comment-based Help is defined by a keyword and each keyword is preceded by a dot ( . ).
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
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
%
can replace Get-ChildItem |
ForEach-Object
{ write-host $_.Name }
which will not work without either the %
or the ForEach-Object
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With