Friday Fun: Open Last File in the PowerShell ISE

Over the last few articles I’ve been sharing some shortcuts to get most recently used or edited files. For today’s Friday Fun I thought I’d share something that I use in my PowerShell ISE profile. Whenever I start the ISE, I automatically open the last file I was working on. Instead of launching the ISE and then finding the most recent file in list under File, this happens automatically.

I found some information on this topic at and revised it to meet my needs. The most recently used files are stored in an XML configuration file, which is good because I can use PowerShell to parse out the information I need. Unfortunately, the path to this file is pretty unwieldy:


Which translates to something like this:


The XML file is called user.config. With that, let’s get some PowerShell in here to get the XML.

The recent used list is stored under user settings.

The value should have what I need.

Looks like it is an array of strings.

Getting closer. It seems the last step I need is that string property.

This should be the same list as I see under File. But while this works, it assumes that the setting I want will always be at position 5 and that I know how to get there in the first place. The smarter way is to use an XPATH filter to find the data. I can use the SelectNodes() method.

The query essentially says find any node that is a <setting> and that has a name attribute of “MRU”. XML is case-sensitive so I have to match what is in the XML file.

With this it is just as easy to get the most recent file.

Once I know the file name it is trivial to load it into the ISE.

The other way I could have selected the node is with Select-XML.

The node property has the setting I need. I can simply keep drilling until I get the value I want.

In this case I’m only selecting the first item. I could have used these code snippets in my profile, but I decided to create a function to retrieve the MRU list.

The function encapsulates everything I’ve shown you. Although I set a default path using a wildcard.

I did this to keep things easier to read and on the off chance that the strong name value might change at some point. By the way I am running this on PowerShell 4.0. I have not tested with v5.

To get the full path, I can resolve this path with a wildcard.

Most everthing else is the same. This function is in my PowerShell ISE profile script and at the end I open the most recent file.

It’s that easy. And because the function is in my profile, I can run it anytime. Although note that the XML file won’t get updated until you close the ISE. Personally, all these little things add up over the course of a day and make my work a bit more fun. Enjoy.

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!

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