Use PowerShell to Find Folders with Specific File Types

Last Update: Sep 04, 2024 | Published: Feb 09, 2015

SHARE ARTICLE

In a recent article I showed you how to identify empty folders with PowerShell and delete them. On a related note I saw a post on Twitter with a Reddit question about getting a list of folder names that contain a certain file type. Interesting. Say I have a folder structure and I want to show the folder names if the folder contains say an MP3 file. Let’s do this thing with PowerShell. First, I’ll define a variable for the file type.

​
It isn't too difficult to get a list of all matching files by using the DIR alias for Get-ChildItem.
​
I selected a few properties so you could see that I have files and their parent directory.
List of files and their parent directory. (Image Credit: Jeff Hicks)
List of files and their parent directory. (Image Credit: Jeff Hicks)
  Notice that the Demo3 folder has multiple files. We'll want to be able to handle that eventually. For now, it seems all I need is the Directory property.
Selecting a directory. (Image Credit: Jeff Hicks)
Selecting a directory. (Image Credit: Jeff Hicks)
  To filter out the duplicates, I can use the –Unique parameter with Select-Object.
Filtering out duplicates with the -Unique parameter and Select-Object. (Image Credit: Jeff Hicks)
Filtering out duplicates with the -Unique parameter and Select-Object. (Image Credit: Jeff Hicks)
  I can even sort the results if I wish.
Sorting results. (Image Credit: Jeff Hicks)
Sorting results. (Image Credit: Jeff Hicks)
  Remember, the result of my PowerShell expression is an object. I am actually getting five objects with a single property called Directory. Imagine I had a custom function that accepted pipelined input that was going to process these folders. Or maybe I want to pass the results to another cmdlet like Get-ACL. I would either need to make sure the next cmdlet had a corresponding parameter that took input by property name, or I might need to rename the outgoing property to something like Path, which many cmdlets use.
Casting the custom Path property in Windows PowerShell. (Image Credit: Jeff Hicks)
Casting the custom Path property in Windows PowerShell. (Image Credit: Jeff Hicks)
  I explicitly cast my custom Path property to be a string to avoid complications when piping to other commands. Now I could do something like this:
Reformatting the Path property after using Get-ACL in Windows PowerShell. (Image Credit: Jeff Hicks)
Reformatting the Path property after using Get-ACL in Windows PowerShell. (Image Credit: Jeff Hicks)
  After using Get-ACL, I reformatted the Path property to make it more user friendly. Otherwise Get-ACL writes a Path property that looks like Microsoft.PowerShell.CoreFileSystem::C:workdemo2Demo3. Maybe all you want is a simple text list that you can save to a file. In other words, you don't want any property names, but just the raw values. Here's one way that sorts the results.
Sorting the results to a simple text list. (Image Credit: Jeff Hicks)
Sorting the results to a simple text list. (Image Credit: Jeff Hicks)
  You could pipe this to Out-File or Out-Printer. But because we're using PowerShell, and it is all about the objects, let me show you a few more things you might do. How about a formatted report?
A formatted report approach. (Image Credit: Jeff Hicks)
A formatted report approach. (Image Credit: Jeff Hicks)
  Or maybe you want to export the results.
​
Remember, that when exporting to a flat file like a CSV, you should specify the properties you want. The selected properties should be simple values and not nested objects. If you need more detail, then simply export to XML.
​
The assumption with Export-Clixml is that you will re-use that data in a future PowerShell session.
Using Import-Clixml in Windows PowerShell. (Image Credit: Jeff Hicks)
Using Import-Clixml in Windows PowerShell. (Image Credit: Jeff Hicks)
  You'll notice that in this command I used DirectoryName and not Directory. These are actually two different types of properties. 011515 1439 FindingFold10 In most of my examples I used the Directory property. As you can see, it is a System.IO.DirectoryInfo type of object. That is why I specifically cast it as a [String] in some of my examples. The property you use depends on what you need to accomplish. I wanted you to realize there were different options and stress how important it is to use Get-Member to identify the correct property. If all you really need is the directory name, then you can most likely use DirectoryName in my examples instead of Directory. Don't make assumptions on the first thing you see. Identifying folders with a certain type of file is pretty easy. What you do with that information can be a little more involved.

SHARE ARTICLE