Tag Archives: Scripting

What PowerShell Script Was I Working On?

Last week I shared a script for finding recently modified files in a given directory. In fact, it wouldn’t be that difficult to find the last files I was working on and open them in the PowerShell ISE. Assuming my Get-RecentFile function is loaded it is a simple as this:

But often, I already have the PowerShell ISE open. True, I could use the same expression substituting ‘psedit’ for ‘ise’, but with the ISE I’m all about working quickly So I have another function targeted for the PowerShell ISE called Open-LastScript.

The core commands in this function are very similar to my other script. And I probably could get by with one script so consider Open-LastScript as a variation on a theme. The function gets PowerShell related files from my Scripts directory. I could have hard coded a default value, but instead I set it to a global variable called ScriptPath which I define in my PowerShell ISE profile script. If that variable isn’t defined, or if nothing is specified for –Path, then the function uses the current location, assuming it is a FileSystem path.

But the fun part, at least for me, is that I take the results of the DIR command and create a few custom properties, including one that shows the number of lines in the script file.

The selected objects and properties are then piped to Out-Gridview. Starting in PowerShell 3.0 you can pass objects back to the pipeline from Out-Gridview. In other words, you can use Out-Gridview as a simple object picker. The function by default gets the 10 most recently modified scripts.

I can select as many entries as I want and click OK. Each selected script will then be opened in the ISE. Yes, I know I can see similar information in the most recently used list but that can show files outside of my Scripts folder and I’m limited to maximum MRU count. My function lets me specify the number of files to list.

The bottom line is that my Open-LastScript function is a handy tool that saves me a little bit of time finding the files I need to work on and every little bit helps. I could create an Add-Ons menu shortcut to run the function with the defaults, but for some reason I opted for an alias.

The alias gives me a little bit of flexibility.

I hope this gives you some ideas of how you can use PowerShell to make your scripting more efficient, or at the very least as an example of PowerShell scripting techniques.

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.