Friday Fun: Get Recent Files

As you might imagine, I work on a lot of different files throughout the week. Sometimes it is hard to keep everything straight. I’ll want to return to script I was working on yesterday, but I can’t remember what I called it. So I spend a few minutes searching and filtering until I find it. That takes too long. So why not create a simple PowerShell tool and load it into my profile?

First, I need a date to filter with. I want to find all files that were last modified yesterday.

If I just used the AddDays method, I would get exactly 24 hours from now, including the time. But I want since midnight. That’s what the Date property gets me. The value of $y is something like Thursday, March 19, 2015 12:00:00 AM. Now it is a simple matter of getting a directory listing and filtering with this date.

I need to make sure I sort the results but do so at the end so that PowerShell is sorting the filtered results.

Naturally I don’t want to have to type that all the time. So here is a function to do the work for me.

I set the default to find all files in the current folder last modified since yesterday. I also added an option to limit the results to the newest X number of files. My scripts folder has 10 years-worth of files so this is very helpful.

I even created an alias.

By the way, if you set the Days parameter to 0 you’ll get files modified today. It also makes it easy to see what I’ve been working on.

I want to point out one other feature that you might find useful in your scripts. You’ll notice that I use parameter validation a lot. If a validation test fails, PowerShell throws an exception. But sometimes you may want something a bit more elegant. In this function, I wanted to validate that the path existed and that it was a FileSystem path. A path like HKLM: would test as True but be invalid for the function’s purpose.

Here’s the validation attribute:

When you use ValidateScript, the scriptblock must return $True or $False. If it is $False then you get the typical exception. In this case, if the path passes my tests I can tell PowerShell it is OK ($True) and the function proceeds. But if the path fails my test I’m going to write my own error message.

As is often the case in PowerShell you have to consider who will be using your tools or script and how will they respond to errors.

The only thing I haven’t done is include any comment based help, so I’ll leave that to you. Enjoy!

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.


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 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.

Advice, solutions, tips and more for the lonely Windows administrator with too much to do and not enough time.