Tag Archives: PowerShell

Pi in the Sky

In celebration of Pi day, I thought I’d post some quick and dirty PowerShell code you can use to calculate pi. I found some easy to follow explanations at http://www.wikihow.com/Calculate-Pi that weren’t too difficult to transform into PowerShell code. And you might even learn something new about PowerShell along the way.

Before we begin, I hope you know that you can always get the value using the [Math] class:

But where’s the fun in that? First up is my PowerShell version of Gregory-Leibniz series. This works by deriving pi from an infinite series.

π = (4/1) – (4/3) + (4/5) – (4/7) + (4/9) – (4/11) + (4/13) – (4/15)…

Seems simple enough. I need a large range of odd-numbered denominators. Then I need to alternately add and subtract. I’m going to need a loop and I can use the modulo operator (%) to test each time through the loop. If I am on an even number I’ll add, otherwise I subtract. Here’s what I came up with.

This takes a little bit of time but it works.

Then I thought I’d try the Nilakantha series.

π = 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8) – 4/(8*9*10) + 4/(10*11*12) – 4/(12*13*14) …

Some of the principals are the same. The tricky part here is looping through the collection of numbers and grouping them.

This is noticeably faster and more accurate, well as far as you can be calculating an irrational number.

And the last way is using an Arcsine Function/Inverse Sine Function:

pi = 2 * (Arcsin(sqrt(1 – x^2))) + abs(Arcsin(x))

This gets a little tricky in PowerShell but it can be accomplished with the [Math] class. You have to watch out for the parentheses.

The value of $x is between -1 and 1.

Also pretty quick, albeit a bit harder on the eyes to read.

Now if you’ll excuse me I have some circles that need to be measured.

Friday Fun: Send PowerShell ISE Content to Word

geekYesterday on Facebook, Ed Wilson was lamenting about confusion of keyboard shortcuts between PowerShell and Microsoft Word. I’ve run into the same issue. Muscle memory is strong. Then the discussion turned to getting content from the PowerShell ISE into a Word document. I humorously suggested we had a plugin and it had a Ctrl+C keyboard shortcut. Then I thought, why not make this even easier!

So I put together a quick function for the PowerShell ISE.

This function will paste any selected text from the ISE into a Word document. The first time you run the function, PowerShell will create a Word document and format it for fixed width text. It will then insert your text and a new paragraph marker. The next time you run the function, it should detect that you have a document open and re-use the existing variables. The Word document will be visible so you can edit it further and save it. If you move the cursor around in the document, any new content you insert will go there.

To make this easy to use, insert this function into your PowerShell ISE profile script and add a menu item with a keyboard shortcut.


Now, I can select code from the ISE script pane and send it to Word with a quick key combination. Have fun and enjoy your weekend.

Update: I posted another version that includes an option to copy and paste as colored code.

PowerShell Blogging Week is Coming

talkbubbleAs I’m sure you are aware there is a lot of great PowerShell goodness online through social media and blogs. Well, I and a few members of the PowerShell community have banded together to add to that goodness. During the week of March 30, look for new daily content from the group. Our topic is advanced PowerShell functions and we will each be contributing a post on our respective blogs covering some aspect of that topic. Naturally, we’ll also be spreading the word through Twitter and other social channels. We hope you will get involved by posting comments or connecting with us on Twitter.

I don’t want to spoil the surprise about what we’ll be covering, but these are the people involved. If you aren’t following them now, you should be:

We will be using the hashtag #PSBlogWeek. This should be a lot of fun and informative so we hope you will join us.