Do you want to sum all numbers in a PowerShell array? In this PowerShell tutorial, I will explain how to sum all numbers in an array in PowerShell.
To sum all numbers in an array in PowerShell, use the Measure-Object cmdlet with the -Sum parameter. For example, $total = ($array | Measure-Object -Sum).Sum will calculate the sum of all numeric values in $array. This is an efficient way to obtain the total of a collection of numbers quickly.
Sum All Numbers in an Array in PowerShell
An array is a data structure that holds a collection of items. These items can be numbers, strings, or any other type of object. In PowerShell, arrays are created by separating items with commas.
$numbers = 1, 2, 3, 4, 5 # This is an array of numbersNow, let us see how to add or sum all these numbers from this array in PowerShell using various methods.
Method 1: Using a ForEach Loop
The simplest way to sum numbers in an array is by using a ForEach loop in PowerShell. This loop iterates through each number in the array and adds it to a sum variable.
$numbers = 1, 2, 3, 4, 5
$sum = 0 # Initialize sum to zero
foreach ($number in $numbers) {
$sum += $number # Add each number to the sum
}
Write-Output "The sum is: $sum"After executing the code using VS code, you can see the output in the screenshot below:

Method 2: Using the Measure-Object Cmdlet
PowerShell provides a cmdlet called Measure-Object that can perform calculations on the properties of objects, including summing numbers in an array.
$numbers = 1, 2, 3, 4, 5
$sum = ($numbers | Measure-Object -Sum).Sum # Use Measure-Object to sum the numbers
Write-Output "The sum is: $sum"The Measure-Object cmdlet is a powerful tool and can be used to calculate other statistical information as well, such as count, average, maximum, and minimum.
Method 3: Using the ForEach-Object Cmdlet
Another way to sum numbers in an array in PowerShell is to use the ForEach-Object cmdlet with the -Begin, -Process, and -End parameters.
Here is a complete PowerShell script:
$numbers = 1, 2, 3, 4, 5
$sum = 0 # Initialize sum to zero
$numbers | ForEach-Object -Begin { $sum = 0 } -Process { $sum += $_ } -End { Write-Output "The sum is: $sum" }In this example, the -Begin block initializes the sum variable, the -Process block updates the sum with each number, and the -End block outputs the total sum.
Method 4: Using Array Accumulation
PowerShell also allows you to directly accumulate an array into a single value by using the += operator inside a loop. This is another method to sum all numbers in a PowerShell array.
$numbers = 1, 2, 3, 4, 5
$sum = 0 # Initialize sum to zero
for ($i = 0; $i -lt $numbers.Length; $i++) {
$sum += $numbers[$i] # Add each number to the sum
}
Write-Output "The sum is: $sum"This method is similar to the ForEach loop but uses a for loop to iterate over the array indices.
Method 5: Using Linq
For those who are comfortable with .NET, PowerShell can use the Linq library to sum numbers in an array.
Add-Type -AssemblyName System.Linq
$numbers = 1, 2, 3, 4, 5
$sum = [System.Linq.Enumerable]::Sum([System.Collections.Generic.List[int]]$numbers)
Write-Output "The sum is: $sum"This method is more advanced and involves casting the array to a generic list that the Sum method can operate on.
Conclusion
In this PowerShell tutorial, I have explained how to sum all numbers in an array in PowerShell using various methods:
- Using a ForEach Loop
- Using the Measure-Object Cmdlet
- Using the ForEach-Object Cmdlet
- Using Array Accumulation
- Using Linq
You may also like:
- Array Comparisons in PowerShell
- How to Compare Two Arrays for Matches in PowerShell?
- How to Create an Object Array with Properties in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.