Category Archives: PowerShell

Send from PowerShell ISE to Microsoft Word Revisited

Many of you seemed to like my little PowerShell ISE add-on to send text from the script pane to a Word document. I should have known someone would ask about a way to make it colorized. You can manually select lines in a script and when you paste them into Word they automatically inherit the colorized tokens. Unfortunately, coming up with a PowerShell equivalent is much more complicated.

If you search around you’ll find plenty of tools and scripts for generating HTML and colorized output from the ISE. I tried incorporating some of them into my script but they were much more complicated than I wanted to deal with. All I really needed was a simple Ctrl+C command. So I cheated. I decided to use the SendKeys() method from VBScript.

I added a new switch parameter to the function called Colorized. This meant I also needed an additional menu shortcut.

You’ll notice that there is no keyboard shortcut. At least for me, I got inconsistent results using a keyboard shortcut, and often nothing. But if I selected the menu item, it always seemed to work.

Here is the complete updated function.

I can’t guarantee the color copy and paste will work 100% of the time. Otherwise, you can always use traditional keyboard shortcuts: Ctrl+C,Alt+Tab (to Word), Ctrl+V.

Enjoy.

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.

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.