Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

PowerShell: Math Functions

📅 2011-Aug-15 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ math, powershell ⬩ 📚 Archive

I keep a PowerShell console open at all times. I find it to be an excellent replacement for the Windows Calculator for quick calculations. Arithmetic operators are part of the PowerShell language, so they can be used directly at the prompt. For mathematical operations like square root, logarithm and those from trigonometry, the System.Math class from the .Net Framework can be used.

For example, to find the square root we can call the Sqrt() method from the System.Math class:

[System.Math]::Sqrt( 20 )

An easier way to do the same is:

[math]::sqrt( 20 )

This shorter case-insensitive version works because PowerShell is designed for an easy console experience. So, in PowerShell all the System.* classes can be invoked without the System prefix. Also, PowerShell tries to be as case-insensitive as possible, so most class and method names can be called ignoring the case.

Tried with: PowerShell 2 Reference: PowerShell in Action (2nd Edition) by Bruce Payette


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧