Delete subdirectory in dir 2 if files dont exist in dir1

I have two parent directories that each have several subdirectories. Inside those subdirectories are several files, but I am only interested in .inf files. Basically, one of the parent directories has a lot of extra subdirectories that are not needed, that contain .inf files that are of no interest to me. So, I would like to be prompted for the path of each parent directory. After that, I will need to find all .inf files in the first parent directory. For each of those .inf files found, I need to find a matching .inf in the second parent directory. I want to keep the subdirectory where the matching .inf file is found. I want to delete all of the extra subdirectories where matching .inf files are not found in the second parent directory. Note that the subdirectory names are not the same in both parent directories. Here is an example of the folder structures.





----a bunch of files, including .inf files










–bunch of file, including .inf files


–bunch of files, including .inf files


This isnt much, but here is what I started with:


$dir1 = Read-Host 'What is the path of the first directory?'
$dir2 = Read-Host 'What is the path of the second directory?'

Get-ChildItem -Path $dir1 -Filter *.inf -Recurse -File -Name | ForEach-Object {

I don’t know how the other Powershellers think but I am struggling to understand what you are trying to achieve. You might describe it again a little more detailed … maybe with one specific example. What’s the situation before and what’s the expected result? Usually we do not recommend it but in this rare case it may be helpful to show a picture. :wink:


I think he is wanting to find the full name of every file in Dir1 and Dir2 and then do a compare-object for the file name. If found in dir2 output it to a new item or entry?

@John Strange,

I’m with Olaf, example and/or further detail is required. Also why are you using the -name parameter, but providing no value? The Filter already takes care of that part.