Weekend Scripter: Search and Filter Directory Lists with PowerShell
Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to sort, search, and to filter through a directory list.
Microsoft Scripting Guy, Ed Wilson, is here. One of my friends on Facebook posted a picture of a bunch of hardwood trees all decked out in fall colors. It was rather pretty. That is the down side to living in central Florida—I mean, I have never seen a palm tree in fall colors. Of course, it is 82 degrees Fahrenheit (28 degrees Celsius), so I guess we don’t really have fall. But the humidity is dropping (down to 55% instead of the usual 75%), so it feels cool outside.
Anyway, I am sitting outside checking email sent to email@example.com on my Surface Pro 3 under a huge ZZ Top oak tree (the trees have long gray beards). I am sipping a cup of Earl Grey tea and munching on a piece of homemade carrot cake.
I like to spend a bit of time exploring the disk usage on my Surface Pro 3. It is not infinite, and even though I bought the largest internal storage and I have an additional little chip for storage, it still fills fast—especially with my Kindle reader and all the whitepapers I like to store. Windows PowerShell scripts take up nothing, but a big fluffy PDF file can easily eat up 25 or 30 Mbytes. With a hundred or so, there went 10% of my storage. Not to mention the ease of downloading apps. So I like to keep an eye on things.
I don’t know what happened to File Manager on my Surface Pro 3, but I really don’t need it because I have Windows PowerShell. I open the Windows PowerShell console, and I use Get-ChildItem to obtain a directory list. I then pipe the output to Out-GridView. Because I am sitting under a ZZ Top tree, balancing my Surface on my knee, and using a virtual keyboard, l am loving the short terse cmdlet aliases. Here is the command:
dir c:\fso | ogv
I can now look at the output in the grid. The command I typed appears in the title of the output grid:
When did all of these files appear? I want to see the files that were last written to since September 1 of this year. I can use my finger to press the Add Criteria button and to check the box beside the LastWriteTime property.
But my fingers seem a bit too wide to make that check very easily, so I use my stylus instead. It is very precise and is designed for making nice little checks in check boxes. So I add that, and then use the handwriting input box to add my date criteria. As you can see here, there is only one file that has been written to in the last month:
Sweet. So now I know that I am not really using the stuff in this folder, and I can easily move it to the cloud and free up some storage on my local device. Luckily, One Drive is quite nicely integrated into Windows 10.
WooHoo! And now I'm off to enjoy our lovely cool central Florida fall.
See you tomorrow…assuming the gators don’t get me.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy