Use PowerShell to Help Find All of Your Images

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to help locate pictures or images on the hard drive.

Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked a little bit about review time at Microsoft. We have to count everything so that we can provide our managers with feedback. Luckily, Windows PowerShell has the Measure-Object cmdlet, so it is pretty easy. Windows PowerShell also works with dates really easily, so it is simple enough to write a script to find all images with a file creation date between July 1, 2011 and June 30, 2012. If you have your hard disk drive organized in such a way that only work related pictures or images reside when you do the search, dude, you are golden.

For me, it is easy to find all of the images or all of the articles I wrote this year. This is because of how my Data folder is laid out. I can view the high-level layout by using the Get-ChildItem cmdlet without the recurse option and filtering for the directories. The Windows PowerShell 2.0 command is shown here.

dir C:\data\ScriptingGuys | where {$_.PsIsContainer}

By using the simple Windows PowerShell 3.0 syntax, the command becomes the following.

dir C:\data\ScriptingGuys | where PsIsContainer

The command and the output associated with the command are shown here.

Image of command output

Simplify things by using a PS Drive

Because the folder layout is so structured, it is easy to find items on a year-by-year basis. For example, I can create a custom PS Drive that is rooted in 2012, and my commands are vastly simplified. The first thing I do is store the current location on the stack by using pushd (alias for Push-Location). Next, I create a new Windows PowerShell drive that is rooted in the 2012 folder. This will simplify my command line and make it easier to work. Now I change to that location (sl is an alias for Set-Location) and finally I use dir (an alias for Get-ChildItem) to find all of the .jpg, .png and .bmp types of files. To count them all, I use the Measure-Object cmdlet (measure is an alias). The commands is shown here.


New-PSDrive -Root C:\data\ScriptingGuys\2012 -PSProvider filesystem -Name hsg

sl hsg:

dir -Recurse -include *png,*jpg,*bmp | measure

The commands and the output associated with the commands are shown here.

Image of command output

If there are 738 images, how many documents have there been this year? A quick change of the Include parameter to *doc and *docx finds that there have been nearly 300 documents this year.

PS hsg:\> dir -Recurse -include *doc,*docx | measure


Count    : 296

Average  :

Sum      :

Maximum  :

Minimum  :

Property : 

Wow! 296 articles have been created. But how many days have there been so far this year? Well, that information is always available from the Get-Date cmdlet, as shown here.

PS hsg:\> (Get-Date).DayOfYear


The difference between the number of Word documents and the number of days is that on many occasions, the Hey, Scripting Guy! Blog publishes more than once a day. For example, during the Scripting Games and during TechEd this year, it was not uncommon to have four or even five postings a day. What is interesting is that I am running an average of 2.5 images per blog this calendar year.

Finding images created during a specific time range

To find images that I created during the time range of July 1, 2011 and June 30, 2012, I need to search multiple folders. This is because I organized my folders by calendar year, not by fiscal year. Therefore, I need to back up a bit in the folder structure. For me, I will back up to the root of the ScriptingGuys folder and begin my search there. The command is a one-liner, and at first I let it run and display all of the images so I can confirm that the command works properly. I decide to sort the results to make it obvious (otherwise the dates begin with October and go sideways in chronology). The trick is to use the compound Where-Object (where is an alias), and the greater than (-gt) and less than or equal (-le) operators to provide only the dates desired. Here is the basic command.

dir -path C:\data\ScriptingGuys -Recurse -include *.png,*.jpg,*.bmp |

where {$_.LastWriteTime -gt [datetime]”7/1/11″ -AND $_.lastwritetime -le [datetime]”6/30/12″}  |  

sort  lastwritetime

The command and the associated output are shown here.

Image of command output

To count the number of images during the past fiscal year, all that I need to do is add the Measure-Object cmdlet. I do not need the Sort-Object cmdlet, so I remove it from the command. The revised command and associated output are shown here.

PS C:\> dir -path C:\data\ScriptingGuys -Recurse -include *.png,*.jpg,*.bmp |

where {$_.LastWriteTime -gt [datetime]”7/1/11″ -AND $_.lastwritetime -le [datetime]”6/30/12″} |  



Count    : 1228

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

Playing with Files Week will continue tomorrow.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (3)

  1. K_Schulte says:

    Hi Ed,

    good to know that Powershell can help me doing my "favorite job": Searching my directories!

    As a universal "find what I need" program is still missing, Powershell can do a good job on this topic!!

    But … searching directories with Powershell 2.0 is really a bit too slow and I hope that the rumor: "PS 3.0

    will improve file access" will become true!!!


  2. Ed Wilson says:

    @K_Schulte PowerShell 3.0 seems faster, but I have not specific timed anything … it is just a feeling. However, I can tell you that the major slowness in dealing with directories is not PowerShell but the .NET Framework itself. For operations where speed is a requirement, I drop back and use the FileSystemObject from the old VBScript days … it is blazingly fast, and is relatively easy to use. I talk about all this in my Windows PowerShell 2.0 Best Practices book from Microsoft Press. I actually do a number of measurements in showing the differences in speed between the different approaches to working with files and folders.

  3. Anil Kumar says:

    How find total memory size of counted images.