What does "%" (percent) do in PowerShell?
When used in the context of a cmdlet (such as your example), it's an alias for ForEach-Object
:
> Get-Alias -Definition ForEach-ObjectCommandType Name Definition----------- ---- ----------Alias % ForEach-ObjectAlias foreach ForEach-Object
When used in the context of an equation, it's the modulus operator:
> 11 % 51
and as the modulus operator, %
can also be used in an assignment operator (%=
):
> $this = 11> $this %= 5> $this1
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
.