Using Percent (%) in Different Contexts
Typically the % character is for mod functionality. But in PowerShell, it’s an alias for ForEach-Objectand can be also used as Modulus Operator and an Assignment Operator (%=). Let’s learn each of them below.“
Use Percent(%) as an alias of the ForEach-Object cmdlet in PowerShell
We can use % sign instead of ForEach-Object cmdlet as % is alias for ForEach-Object cmdlet.
Get-Alias -Definition ForEach-Object
CommandType Name Alias % -> ForEach-Object Alias foreach -> ForEach-Object
The above command will list all of the aliases for the cmdlet ForEach-Object. In PowerShell, the alias is a short and alternate name for a function, cmdlet, or script. For example, the ForEach-Object cmdlet is used to perform a specific operation on every item in a collection of input objects, and it is an alias for % and foreach.
Let’s see with the help of example:
30, 20, 10 | ForEach-Object -Process {$_/10}
3 2 1
This example takes an array of three integers and divides each one of them by 10.
30, 20, 10 | % -Process {$_/10}
3 2 1
As we can see, we replaced ForEach-Object with % and it worked in same way.
Use percent (%) as the Modulus Operator in PowerShell
13 % 5
3
Here, the % is a modulus operator when applied to an equation; we ran the above command to check this.
Use percent(%) as an assignment operator (%=) in PowerShell
$num=13 $num %= 5 $num
3
That’s all about what does percent mean in PowerShell.