Windows Server How-To
How To Do Math Using PowerShell, Part 1
Be warned: some simple .NET work is involved.
Most of the PowerShell scripts that I have seen over the years are relatively straightforward. They tend to use a sequence of cmdlets and various operators and parameters. However, a script consisting only of these types of elements is somewhat limited in what it can be used for. Don't get me wrong ... Microsoft has given us thousands of PowerShell cmdlets and you can use them to accomplish some impressive things. However, if you really want to use PowerShell as a programming language then it is important to know how to do PowerShell math. To give you a more concrete example, I recently saw a really cool PowerShell script that used various mathematical functions to predict system resource depletion.
As a general rule, if you want to perform math through PowerShell, you can just type the problem as you would enter it into a calculator. Need to know what 2 + 2 is? Just type 2+2 into PowerShell and PowerShell gives you the answer, as shown in Figure 1.
Other math problems work just as well. PowerShell allows you to enter things like:
2+2
5-1
2*3
6/3
And even 2 + (3*3)
You can see how easily PowerShell handles basic arithmetic in Figure 2.
Although PowerShell makes it easy to do arithmetic, things get a little more interesting when you start trying to do anything more advanced. Want to know what 2 squared is? Try entering 2 ^ 2 and you will get an error message like the one shown in Figure 3. The same goes for just about any other mathematical expression that you can think of.
I seem to recall someone once telling me that PowerShell is completely incapable of performing any sort of math beyond the most basic arithmetic. Contrary to popular belief, however, you can do other forms of math through PowerShell.
The trick to being able to do more than just basic arithmetic is that you have to delve into a dark art known as .NET. Now before those of you who are not developers tune out, let me assure you that this is a lot easier to accomplish than it sounds. In a moment I will show you how it works.
.NET groups collections of functionality into something called an assembly. An assembly typically contains multiple classes. A class can be thought of as an individual unit of functionality. For instance if we were building an assembly for basic math (we don't actually have to do this, I am just using basic math as an example) we might have a class for addition, another class for subtraction, and the list goes on.
One more component that you need to know about before I move on is a namespace. Namespaces can become complicated, but for our purposes you can think of a namespace as a label that allows classes from multiple assemblies to be addressed using a common name.
So what does all of this have to do with doing math through PowerShell? Well, PowerShell is only able to natively perform the most basic math. However, there is a .NET class called Math that we can use to perform more advanced math through PowerShell. To be technically precise, the class name is System.Math, but PowerShell usually allows you to omit the System part of the class name. You can read the full specs for the class here.
OK, so enough of the .NET lesson. I think that the real question is how you can call a .NET class without being a professional developer.
Remember when I said that it was easy to call a .NET class from PowerShell? I wasn't lying. All you have to do is to enclose the class name in brackets, enter two colons and then enter your mathematical expression. Suppose, for instance, that I wanted to know the square root of 64. The first thing that I would need to do is to specify the class name. It's technically System.Math, but we can just use Math. The class name looks like this: [Math]. Next we type two colons. Hence, any line of code that makes use of the Math class will start out like this:
[Math]::
It doesn't matter if you are calculating a square root or retrieving the value of PI, [Math]:: will always come before the expression.
So all you need now is the expression. In this case it's SQRT(64). As such, the line of code looks like this:
[Math]::SQRT(64)
You can see the result in Figure 4.
So as you can see, PowerShell makes it really easy to do math so long as you know how to tap into the .NET System.Math class. Tune in for the second part, coming soon.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.