Use PowerShell to Find Folders with Specific File Types
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. 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.
To filter out the duplicates, I can use the –Unique parameter with Select-Object.
I can even sort the results if I wish.
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.
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:
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.Core\FileSystem::C:\work\demo2\Demo3. 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.
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?
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.
You'll notice that in this command I used DirectoryName and not Directory. These are actually two different types of properties. 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.