Powershell |% is an alias for foreach commandlet. The % will execute a script block for each of the objects passed to it through the pipeline (|). This lets us some script run against each item in a list.
It is often used with $_ which is also an alias that carries the item corresponding to a particular iteration of the foreach cmdlet. The following examples show how to use this.
- PowerShell Foreach |%
- More examples of PowerShell |%
- PowerShell |% while reading from a file.
People Also Search For:
Consider the PowerShell script as shown below.
Here we create an arrray $a with elements 1 to 10. Then we loop through the array using the foreach() command let to display the elements one by one.
We are having to create a new variable called $i to temporarily store the current element of the array in each iteration.
Now let us carry out the same process using the Powershell |%.
PowerShell |% foreach:
Consider the following script snippet for iterating through an array and displaying the elements.
The output of the above snippet is shown in the image below.
Clearly it does the exact same task as the script that contains the foreach command let but it is much shorter and easier to read and understand. Beginners might disagree but as they get more familiar with it they will soon appreciate the efficiency of the alias.
We can take this a step further using the below script.
The output of the above script is given in the image below.
Clearly this one-liner is the most efficient example shown here. The best thing about PowerShell |% is that it just works every time.
More examples of PowerShell |%
We can use PowerShell |% everywhere we use the foreach command let. The below script will try to get the list of processes that are only related to chrome.
The output of the script is shown in the image.
But on the side note, we can achieve the above task in a much easier way using the script given below.
The output of this script is given in the image.
PowerShell |% can be really useful while reading content from files.
PowerShell |% while reading from a file
For this example, we will wite the content of the get-process to a file called processes.txt. We will read the content of the file and check if the line contains the word ‘chrome‘ in it and if it has it, we will print the output to the screen.
The below script will write the content of the get-process to the file.
Now we will read the contents of the file and check for the word ‘chrome‘ in it and if it has it, we will print it to the screen.
The output is given in the image
There are many examples that can be given for PowerShell |% and we have given some of the most used cases here. We have learned that PowerShell |% is essentially the alias for PowerShell for each and it is combined with $_ to get to each item in the specific iteration. This is a very useful syntax because it will decrease the amount of a script written to achieve something.
For amazing posts on PowerShell stay tuned to Teckangaroo.
- Author Rating
- Content Clarity
- Content Relevance
- Article Rating