PowerShell Pie for Pi Day

PowerShell Pie for Pi Day

  • Comments 1
  • Likes

Summary: Use the built in pi value in an impromptu function to get the circumference of a circle by using Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are still pretty geeked out from the first ever Windows PowerShell Saturday that was held in Columbus Ohio. Add that today will be the third live meeting in the PowerShell Essentials for the Busy Admin series, and the week is pretty complete. But, as they say on late night television (at least in the United States), “Wait! There is more!” I completed writing all the events for the 2012 Scripting Games, and I emailed them off to the 20 guest commentators. I completed the 2012 Scripting Games quiz and got it sent off to the people who will turn it into an online quiz. And in just a few days, it will be International PowerShell User Group day! Now that ought to be enough to make any geek or geekette jump up and down with excitement.

How to get pi in PowerShell

I know I wrote a blog about not using the .NET Framework classes—but I was talking about unless you need to do so. Well, the easy way to get access to pi is to use the .NET Framework Math class. Pi is available as a static property, which means it is always available. I know that pi is equal to 3.14, or 22/7, but that is about it. Beyond the first few digits, it starts to get fuzzy for me. But I do not need to remember it because I can use the static pi property from the Math class. The following example illustrates using the pi property from the Math class to obtain a more precise value than simply 3.14.

PS C:\> [math]::pi

3.14159265358979

The key to using a static property from a .NET Framework class is remembering to use the double colon symbol (::). If you are wondering how I found this property, it is easy—I used the Get-Member cmdlet. This technique is shown here.

[math] | Get-Member –Static

The command and the associated output are shown here.

Image of command output

Suppose I need to calculate the circumference of a circle. In fact, I do this quite often in my woodworking shop. I cut out a circle for a tabletop, and I want to add an edge band to the circle so that it does not display open-pored end grain. Well, I need to know the circumference of the circle so I know how long of a piece of wood to cut. I could use paper and pencil, and perform this calculation:

Circumference = 2πr

Creating an impromptu function that uses pi

One of the cool things I can do in Windows PowerShell is creating an impromptu function. I do not need to open the Windows PowerShell ISE, type out a long convoluted script containing a function, then save it and dot source it into my Windows PowerShell console. Instead, I can create the function on the fly and use it throughout my session.

function get-circumference { Param($r)2*[math]::pi*$r }

Image of command output

Instead of using the Function keyword, I can also create the function directly on the function drive. To do this, I use the New-Item cmdlet. The code to do this is shown here.

New-Item -path function:get-circumference -Value {Param($r)2*[math]::pi*$r}

The code to create the Get-Circumference function directly on the function drive and the associated output are shown in the image that follows.

Image of command output

Well, that is about all there is to using pi to get the circumference of a circle. I need to get to work on prepping for today’s live meeting. See you then. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • # You can calculate Pi with Powershell as well if you feel like it.

    # This script calculcates Pi using Viete's forumula.

    # en.wikipedia.org/.../Vi%C3%A8te%27s_formula

    # 2/pi = sqrt(2)/2 * sqrt(2 + sqrt(2))/2 * sqrt(2 + sqrt(2 + sqrt(2))/2 ....

    function calctop ($level)

    {

     if ($level -eq 1)

     {

       $returnval = [Math]::Sqrt(2)

     }

     else

     {

       $returnval = [Math]::Sqrt(2 + (calctop($level - 1)))

     }

     $returnval

    }

    $starttime = get-date

    $total = 1

    for ($i = 1; $i -le 500; $i++)

    {

     $result = calctop($i)

     $rhs = $result / 2

     $total = $total * $rhs

    }

    $pi = 2 / $total

    write-output "Pi is $pi"

    $endtime = get-date

    $timediff = $endtime - $starttime

    write-output "Start Time: $starttime"

    write-output "End Time: $endtime"

    write-output "Time Difference: $timediff"