When working with PowerShell, one of the most commonly used cmdlets for calculations is Measure-Object. It helps calculate values such as count, sum, minimum, maximum, and average. However, a frequent issue developers and system administrators face is that the result often contains too many decimal places, making it hard to read or use in reports.
In this tutorial, you’ll learn exactly how to display Measure-Object results with 2 decimal places in PowerShell. We’ll cover what Measure-Object does, why formatting decimals is important, and the best techniques such as [math]::Round() and PowerShell’s -f string formatting.
What is Measure-Object in PowerShell?
The Measure-Object cmdlet is built into PowerShell and allows you to perform statistical calculations quickly. With it you can:
- Count the number of objects.
- Find the minimum and maximum values.
- Sum up numeric properties.
- Calculate averages for numeric data.
Here’s a simple example:
Get-Content numbers.txt | Measure-Object -Property Length -Average -Sum -Minimum -MaximumThis command reads the file numbers.txt and calculates numeric statistics based on the length of each line.
However, if your numbers contain decimals, you may notice the output showing very long results such as 12.34567891234. While that’s mathematically accurate, it’s not always user-friendly. Most reporting or financial systems, for example, prefer numbers rounded to two decimal places.
Check out PowerShell Round to 2 Decimal Places
The Problem with Decimal Places
By default, PowerShell doesn’t format decimal precision in Measure-Object. Let’s take an average example:
$result = (1..10 | Measure-Object -Average).Average
$resultOutput might look like:
5.5That’s fine. But try a more complex dataset:
$result = (10.123, 20.567, 30.789 | Measure-Object -Average).Average
$resultOutput:
20.4933333333333333This looks messy and isn’t ideal if you want to present clean results in a table, CSV report, or monitoring dashboard.
Check out How to Test If a File Exists in PowerShell?
How to Format Measure-Object Results to 2 Decimal Places
Thankfully, PowerShell provides several ways to control the number of decimal places in your output. Let’s go through the most common techniques.
Method 1: Using String Formatting with -f
The -f operator in PowerShell allows you to format numbers just like you would in .NET string formatting. The "{0:N2}" format specifier means “convert the number into a string with 2 decimal places”.
$result = (Get-Content numbers.txt | Measure-Object -Average).Average
"{0:N2}" -f $resultOutput:
12.35Key points:
- Always rounds to 2 decimals.
- Outputs as a string (use for display or reporting).
- Best for scenarios where readability matters, like printing results to the screen or formatting reports.
Check out Get the Type of an Object in PowerShell
Method 2: Using [math]::Round()
If you need to keep the result as a numeric type (double) rather than converting it to a string, use the .NET Math class:
$result = (1..5 | Measure-Object -Average).Average
[Math]::Round($result, 2)Output:
3Here is the exact output in the screenshot below:

Here, the average of numbers 1 through 5 is 3.0, but since we forced 2 decimal places, it shows neatly.
For a less clean average:
$data = 10.125, 5.786, 3.919
$avg = ($data | Measure-Object -Average).Average
[Math]::Round($avg, 2)Output:
6.61Key benefits:
- Keeps the number as numeric, not string.
- Ideal for further calculations after rounding.
Read Find the Index of a String in an Array in PowerShell
Method 3: Truncation Instead of Rounding
Sometimes you don’t want to round, but strictly cut decimals after two digits (truncate). PowerShell doesn’t have direct truncation formatting, but you can achieve it using math:
$value = 12.345
[Math]::Truncate($value * 100) / 100Output:
12.34Use this in financial or strict-log scenarios where rounding isn’t allowed.
Read PowerShell Convert Secure String to Plain Text
PowerShell Measure-Object 2 Decimal Places: Examples
Example 1: Average File Size in MB with 2 Decimals
Here is an example to get the average file size in MB with 2 decimals.
$files = Get-ChildItem "C:\Logs"
$avgSize = ($files | Measure-Object -Property Length -Average).Average / 1MB
"{0:N2}" -f $avgSizeOutput:
2.45This formats the average file size in megabytes to exactly two decimal places. Perfect for storage reports.
Example 2: Summing Transactions
For financial values, you often need exactly two decimals.
$transactions = 10.125, 5.786, 3.919
$sum = ($transactions | Measure-Object -Sum).Sum
"{0:N2}" -f $sumOutput:
19.83No messy floats, just two decimals like an accounting system expects.
Example 3: CPU Usage Logs
Suppose you have a log file containing CPU percentages.
$cpuUsage = Get-Content cpu_log.txt
$avgCPU = ($cpuUsage | Measure-Object -Average).Average
[Math]::Round($avgCPU, 2)Output:
45.12Exactly two decimals make it easy to report or chart performance data.
Example 4: Formatting for Tables and Reports
Sometimes you want to display multiple object properties with fixed decimal formatting. Use calculated properties and Format-Table:
Get-Process |
Select-Object Name,
@{Name="CPU(s)";Expression={"{0:N2}" -f $_.CPU}} |
Format-Table -AutoSizeThis ensures all CPU numbers line up neatly with two decimals, improving readability in reports.
For exporting to CSV:
$data = 1..10 | Measure-Object -Average
[PSCustomObject]@{Average = [Math]::Round($data.Average, 2)} |
Export-Csv "report.csv" -NoTypeInformationThis keeps the numeric value as a number for Excel but limits it to two decimals.
Read Add-Content in PowerShell
Best Practices for Decimal Formatting
- Use
[math]::Round()when you’ll reuse the number in calculations. - Use
"{0:N2}" -ffor human-readable output in tables or reports. - Truncate only when required by policy, since rounding is more natural.
- Be consistent throughout your script to avoid confusion.
- Always divide file sizes into MB/GB before formatting for readability.
Common Mistakes to Avoid
- Expecting Measure-Object to have a
-Decimalsswitch (it doesn’t). - Converting to strings too early, then trying to do math later.
- Forgetting to divide by
1MB,1GB, etc., leading to huge unreadable numbers. - Not handling different culture settings (comma vs dot for decimals).
In this tutorial, I explained how to display Measure-Object 2 decimal places in PowerShell using various methods with examples.
- Use
"{0:N2}" -f valuefor display formatting. - Use
[math]::Round(value, 2)for numeric calculations. - Use truncation if strict cutoff is needed.
You may also like the following tutorials:
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.