Tag Archives: Scripting

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.

Friday Fun: Size Me Up

Part of day job involves creating training material, often in the form of video training for Pluralsight or articles for Petri.com. Since I usually am covering PowerShell I often need to capture a PowerShell session. And sometimes I want the screen to be a particular size. So over time I’ve created a few PowerShell tools to resize console and application windows. The PowerShell console window stores its dimension under $host.ui.rawui.windowsize.

These are the same settings you would see here:

As long as you use a value less than the buffer dimensions, you can modify the console window from a prompt. But it takes a step you might not realize. You can’t do this:

Instead, you can create a new type of object with your intended dimensions.

Then you can use this object as a value for the WindowSize property.

Naturally, I created a function to do this for me.

My function also includes code to support –WhatIf.

Of course now that I’ve shown you that I have an alternative. You can use the .NET class [System.Console] which has properties for width and height. And you can set these values independently.

You can’t discover this unless you know something of the .NET Framework, but you could have discovered $host which is why I showed you that first. Since I often need to record video at 1280×720 dimensions, I wrote a quick and dirty script to set my PowerShell console window to those dimensions.

Everything I’ve shown you so far is for the PowerShell console. But what about the ISE? You can’t use the techniques I’ve covered. Application windows are bit more complicated and I’m not going to go into the details. But I came across some code on GitHub (https://gist.github.com/coldnebo/1148334). I don’t do Minecraft but it didn’t take much to turn it into a re-usable function.

The code supports –WhatIf and defaults to the current application, which is presumably the PowerShell ISE.

But this is what actually gets set.

So if you wanted to include the title bar you would need to adjust accordingly.

All of this may not really be applicable to your work, but if you find a good use I hope you’ll let me know. Have a great weekend.