Modulus Operator

You can use the Modulus Operator ‘%’ in a few ways that can be very helpful depending on what you are trying to achive. Modulus can be thought of as a remainder when one number divides another. For instance 5 Mod 3 is 2 and 5 Mod 6 is 5.

 #5 Divided by 3 leaves a remainder of 2
5 % 3

#5 Divided by 6 leaves a remainder of 5
5 % 6

Find Multiples of x
To find multiples of a given number you can simply find all numbers in an array that have a remainder of 0 when divided by x

 $arr = {Array](1..100)
#Find all multiples of 3
$arr | ?{$_ % 3 -eq 0}

Find every xth item
When not working with numbers you can also use % to find every 2nd, 3rd, … xth item in an array by using $i as a counter. Bare in mind 0 is the first element of an array which can become confusing in this example

 #Get the first value and every xth value thereafter
$arr = 'one','two','three','four','five','six','seven','eight','nine','ten'
$i = 0
$x = 2
While ($i -lt $arr.Length){
    if(($i % $x) -eq 0){


Leave a Reply

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