Have you ever run into a problem where the results from a PowerShell command are returned in scientific notation? I’ve recently been working with performance counters in PowerShell and I’ve run into several scenarios where this occurs such as the one shown in the following example.
(Get-Counter -Counter '\PhysicalDisk(*c:)\Avg. Disk sec/Read' -OutVariable Results).CounterSamples
In addition to returning the results in the previous example, they were also stored in a variable so the same value could be used throughout this blog article.
Windows PowerShell version 5.1 is used in the examples shown in this blog article. The performance counter cmdlets do not exist in PowerShell Core.
Only the value for the performance counter itself is returned in the following example.
To convert the scientific notation value shown in the previous example to decimal, remove “E-05” from end of it and move the decimal point left the number of places specified by the notation. That would be five decimal places in this scenario. If the notation were positive, it would need to be moved to the right that many decimal places.
While you could write a complicated function to perform the conversion, the simplest way is to cast it as a decimal.
$Results.CounterSamples.CookedValue -as [decimal]
A second way of casting it as a decimal is shown in the following example.
As referenced by Wikipedia, Scientific Notation is a way of expressing numbers that are too big or too small to be conveniently written in decimal form.