PowerShell Measure-Object 2 Decimal Places [With Examples]

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 -Maximum

This 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
$result

Output might look like:

5.5

That’s fine. But try a more complex dataset:

$result = (10.123, 20.567, 30.789 | Measure-Object -Average).Average
$result

Output:

20.4933333333333333

This 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 $result

Output:

12.35

Key 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:

3

Here is the exact output in the screenshot below:

PowerShell Measure-Object 2 Decimal Places

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.61

Key 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) / 100

Output:

12.34

Use 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 $avgSize

Output:

2.45

This 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 $sum

Output:

19.83

No 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.12

Exactly 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 -AutoSize

This 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" -NoTypeInformation

This 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}" -f for 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 -Decimals switch (it doesn’t).
  • Converting to strings too early, then trying to do math later.
  • Forgetting to divide by 1MB1GB, 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 value for display formatting.
  • Use [math]::Round(value, 2) for numeric calculations.
  • Use truncation if strict cutoff is needed.

You may also like the following tutorials:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.