Windows Server How-To

How To Do Math Using PowerShell, Part 2

We build on part one to find more math operators in .NET that can be used in PowerShell.

In a recent post, I showed you how to use PowerShell to perform mathematical calculations that exceeded PowerShell's native capabilities. The method that I used involved tapping into a .NET class called System.Math. If you read the previous post then you know that accessing .NET classes is relatively simple to do.

As I finished writing that post, I realized that there was more that I wanted to say about making use of .NET classes. After all, I provided an example of using a .NET class to perform a mathematical calculation and I also provided a link to a TechNet article that listed the various methods that make up the class. However, I wanted to take things a little bit further by showing you how to see which methods are available to you.

Based on the fact that you are reading an article about tapping into .NET from PowerShell, I am assuming that you know that PowerShell cmdlets are normally made up of verb–noun combinations. With that in mind, Microsoft gives us a few different ways to get help with command availability and command syntax.  Suppose, for instance, that we wanted to know all of the native PowerShell cmdlets that use the noun PhysicalDisk. To get this information, we could use the following command:

Get-Command *-PhysicalDisk

As you can see in Figure 1, there are five commands based on the PhysicalDisk.

[Click on image for larger view.]  Figure 1. Get-Command can be used to retrieve a list of native PowerShell cmdlets.

PowerShell doesn't limit you to merely getting a list of commands. You can also retrieve a command's syntax by using the Get-Help cmdlet. Simply append the cmdlet for which you want to get help. For instance, if you wanted to know how the Add-PhysicalDisk cmdlet worked, you could type:

Get-Help Add-PhysicalDisk

You can see what the results look like in Figure 2.

[Click on image for larger view.]  Figure 2. You can retrieve a command's syntax by using the Get-Help cmdlet.

Although the Get-Command and the Get-Help cmdlets work really well for use with native PowerShell cmdlets, they don't work with commands that are part of .NET classes. As you may recall, we referenced the System.Math class by using [Math]::<method name>. There isn't really a logical way to adapt the Get-Help or Get-Command cmdlets to work with this syntax.

So what do you do if you want to find out what methods are available for you to use? Well, you can use a command called GetMethods. No, that wasn't a typo. Unlike native PowerShell cmdlets, GetMethods is based on .NET and does not make use of a hyphen. Here is how it works:

[Math].GetMethods()

The only problem with the command listed above is that it results in information overload. If you look at Figure 3, you can see that it displays all of the available methods, but it also displays quite a bit of information for each method. The end result is that there is way too much information to view on screen.

[Click on image for larger view.]  Figure 3. The [Math].GetMethods() command results in information overload.

The easiest way to make the results more manageable is to filter the list to show only the method names. Fortunately, we can revert to using native PowerShell for this. The command that we would use is:

[Math].GetMethods() | Select-Object Name

Select-Object is a native PowerShell cmdlet and allows individual cmdlet properties to be displayed. In this case, we are using it to display all of the methods that exist within the System.Math class. You can see the results in Figure 4.

[Click on image for larger view.]  Figure 4. The result list contains a lot of duplicate methods.

As you can see in the figure above, there are numerous instances of some of the methods. For instance, we have a lot of methods named Max. So let's get rid of the redundancy. To do so, we need only to append the Unique switch to the end of the command. The command therefore becomes:

[Math].GetMethods() | Select-Object Name –Unique

You can see the much more manageable result list in Figure 5.

[Click on image for larger view.]  Figure 5. Here is a list of the unique Math methods.

The result list shown in the figure above is essentially a list of the math operators that we can use. For instance, we could use the Round method to round a number. You can see an example of this in Figure 6.

[Click on image for larger view.]  Figure 6. You can use the Round method to round a number.

As you can see, the System.Math class makes it possible to go way beyond PowerShell's built-in math capabilities. Learning to use the System.Math class opens the door to creating much more powerful PowerShell scripts.

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.

Featured

comments powered by Disqus

Subscribe on YouTube