WindowsWindows Loving ItLoving It

PowerShell ForEach Tutorial with Math & Build Commands [Latest 2020]

PowerShell tutorial for foreach command with math functions and other windows inbuilt command lets. Use it to make PowerShell scripts iterate again.

ForEach in PowerShell

Looping is one of the most basic concepts of all programming languages.  PowerShell is no different in that case either.  In this post, we are going to show you how to use ForEach loop in PowerShell.

Jump to:

PowerShell variables and arrays:

To begin understanding windows PowerShell looping, we need to ensure that we already understand variables in PowerShell.  Here is an example that stores the value 5 in a variable.

$a = 5
PowerShell variables
PowerShell variables

This line alone is enough to store the value of 5 in a variable $a.

Now let us see how easy it is to make and view arrays of variables in PowerShell.  For this purpose, we need to create multiple variables just like we did earlier with $a.

$b = 6

$c = 7

more PowerShell Variables
more PowerShell Variables

We will create an array $array using the below command.

$array = $a,$b,$c

And we can view the contents of $array using the below command.

PowerShell arrays
PowerShell arrays

We can access the specific element by using its position with the help of box brackets as shown below [].




elements using the position
elements using the position

Note that, the number inside the box bracket is the position and the first position is 0 and so on.

If we want to add the number 5 each one of the variables we can do it easily with the below commands.




But if we attempt that on the array we get unexpected results as shown below.

adding an element to array
adding an element to array

To add number 5 to each of the elements in the array, we need to use the ForEach command in PowerShell.

Mathematical Operations using ForEach:

To be able to use the foreach command, we need to keep the following things in mind.

  • We need to invoke the ForEach command using the foreach keyword.
  • Use the parenthesis () for holding the placeholder variable for the array.
  • The commands that should be executed for each of the elements should be enclosed in curly braces {}.

The below command will add number 5 to all the elements in the array.

foreach($i in $array){

foreach in powershell
foreach in powershell

Here the $i is the place holder for each of the elements in the array.  We can see that $i holds the value for the first element in the first iteration.  It holds the value of the second element in the second iteration and finally, it holds the value of the third element in the third iteration.

If the first iteration, the value is 5 and so 5+5 is 10.  The second iteration has $i holding 6 (because 6 is the value in the second position or rather the second element or whichever you may like to call it.) and 6+5 is 11.  The final iteration $i has 7 because 7 is the third or the final element in the array and 7+5 is 12.

We get the expected output.

Let us just take this one step further by adding one more element to the array and instead of adding, we will multiply each element by 10.

We can add a new element to the array using the command.

$array = $array+8

Let’s view the contents of the array using $array

Now we will modify the foreach such that, it will multiply each element with 10 as shown in the below command.

foreach($i in $array){

multiplication using foreach
multiplication using foreach

See how we have removed $i+5 and replaced it with $i*10.

We get the above output.  Which is expected of the foreach statement.

ForEach using built-in command lets:

We can do much more than this with PowerShell foreach like we can get the logical disks using PowerShell and WMI objects.  We can use the following loop to do the same.

$dsk= Get-WmiObject Win32_LogicalDisk
ForEach ( $drv in $dsk ) { “Drive = ” + $drv.Name}

PowerShell foreach
PowerShell foreach

Just like earlier examples, we got the disk details in the array (or collection) called $dsk and used the foreach each command to display each one of them with a string prefix “Drive”.

Similarly, we will use the following script to get the details of the running Nvidia process that starts with nvd.

ForEach ($T in Get-Process nvd*) {$T}
getting nvd* processes
getting nvd* processes

We can get only the name using the foreach loop by using the modified script.

ForEach ($T in Get-Process nvd*) {$T.processname}
NVD processes
NVD processes


The foreach PowerShell is a very useful command that works in various situations.  We can do mathematical operations on arrays and also perform useful operations on the build commands like get-childitem and also get-process.

There is a variant on foreach known as the foreach-object.  For information on that, check out TecKangaroo.

Leave a Reply

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