Table of Contents
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.
|
1 2 3 |
Get-Alias -Definition ForEach-Object |
|
1 2 3 4 5 |
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:
|
1 2 3 |
30, 20, 10 | ForEach-Object -Process {$_/10} |
|
1 2 3 4 5 |
3 2 1 |
This example takes an array of three integers and divides each one of them by 10.
|
1 2 3 |
30, 20, 10 | % -Process {$_/10} |
|
1 2 3 4 5 |
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
|
1 2 3 |
13 % 5 |
|
1 2 3 |
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
|
1 2 3 4 5 |
$num=13 $num %= 5 $num |
|
1 2 3 |
3 |
That’s all about what does percent mean in PowerShell.