WindowsWindows Loving ItLoving It

[BEST] What is PowerShell |% examples, shortcuts, file read – 2020

The best examples and quick start gudie on Powershell |% for beginners. Examples on loops, shortcuts, file read, get process and more.

PowerShell percentage sign

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.

Jump to:

People Also Search For:

Consider the PowerShell script as shown below.

$a = 1..10

foreach($i in $a){

for each powershell
for each powershell

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.

$a = 1..10

$a | %{$_}

The output of the above snippet is shown in the image below.

powershell |%
powershell |%

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.

1..10 | %{$_}

The output of the above script is given in the image below.

array to PowerShell |%
array to PowerShell |%

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.

gps |%{if($ -match “chrome”){$_}}

The output of the script is shown in the image.

get chrome process
get chrome process

But on the side note, we can achieve the above task in a much easier way using the script given below.

gps chrome

The output of this script is given in the image.

get process chrome
get process chrome

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.

gps >> processes.txt

get process to text file
get process to text 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.

${E:\pwrshl\processes.txt} | % {if($_ -match ‘chrome’){$_}}

The output is given in the image

get the process from the txt file
get the process from the txt file


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.

powershell |%
  • Author Rating
  • Content Clarity
  • Content Relevance
  • Article Rating

Leave a Reply

Your email address will not be published. Required fields are marked *