{"id":10781,"date":"2012-03-14T00:01:00","date_gmt":"2012-03-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/03\/14\/powershell-pie-for-pi-day\/"},"modified":"2012-03-14T00:01:00","modified_gmt":"2012-03-14T00:01:00","slug":"powershell-pie-for-pi-day","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-pie-for-pi-day\/","title":{"rendered":"PowerShell Pie for Pi Day"},"content":{"rendered":"<p><strong>S<\/strong><b>ummary<\/b>: Use the built in pi value in an impromptu function to get the circumference of a circle by using Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are still pretty geeked out from the first ever <a href=\"http:\/\/powershellgroup.org\/content\/powershell-saturday\" target=\"_blank\">Windows PowerShell Saturday<\/a> that was held in Columbus Ohio. Add that today will be the third live meeting in the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/03\/06\/windows-powershell-for-the-busy-admin.aspx\" target=\"_blank\">PowerShell Essentials for the Busy Admin<\/a> series, and the week is pretty complete. But, as they say on late night television (at least in the United States), &ldquo;Wait! There is more!&rdquo; 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.<\/p>\n<h2>How to get pi in PowerShell<\/h2>\n<p>I know I wrote a blog about not using the .NET Framework classes&mdash;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 <b>Math<\/b><i> <\/i>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 <b>Math<\/b><i> <\/i>class. The following example illustrates using the pi property from the <b>Math<\/b><i> <\/i>class to obtain a more precise value than simply 3.14.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [math]::pi<\/p>\n<p style=\"padding-left: 30px\">3.14159265358979<\/p>\n<p>The key to using a static property from a .NET Framework class is remembering to use the double colon symbol (<b>::<\/b>). If you are wondering how I found this property, it is easy&mdash;I used the <b>Get-Member <\/b>cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">[math] | Get-Member &ndash;Static<\/p>\n<p>The command and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7180.hsg-3-14-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7180.hsg-3-14-12-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">Circumference = 2&pi;r<\/p>\n<h2>Creating an impromptu function that uses pi<\/h2>\n<p>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.<\/p>\n<p style=\"padding-left: 30px\">function get-circumference { Param($r)2*[math]::pi*$r }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1780.hsg-3-14-12-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1780.hsg-3-14-12-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Instead of using the <b>Function<\/b> keyword, I can also create the function directly on the function drive. To do this, I use the <b>New-Item <\/b>cmdlet. The code to do this is shown here.<\/p>\n<p style=\"padding-left: 30px\">New-Item -path function:get-circumference -Value {Param($r)2*[math]::pi*$r}<\/p>\n<p>The code to create the <b>Get-Circumference<\/b> function directly on the function drive and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3324.hsg-3-14-12-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3324.hsg-3-14-12-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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 <a href=\"https:\/\/msevents.microsoft.com\/CUI\/EventDetail.aspx?EventID=1032508053&amp;Culture=en-US\" target=\"_blank\">today&rsquo;s live meeting<\/a>. See you then. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,3,4,45],"class_list":["post-10781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/10781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=10781"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/10781\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=10781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=10781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=10781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}