Check multiple subdirectories and delete if file exists Long story short, I have over 180K files that I need to delete. I have all of the file names that need to be deleted in a TXT file, however the files that need deleting are scattered in over 400 folders within this one directory. 

I'm attempting to design a script that reads a .TXT file, checks if the file exists in any of the sub directories and deletes if found. 

I'm quite knew to PowerShell / Object-oriented scripts so need some assistance.  

$WorkingDir = 'D:\TEST\'
$txtFile = Get-Content "D:\TEST\Files.txt"

#foreach file in the .TXT
foreach ($file in $txtFile){
    # foreach file in the folders
    foreach($dir in (Get-ChildItem $WorkingDir -Recurse)){
        # if the file name is in directory listed, then delete?
            If (Test-Path $file){
                Remove-Item $file
                  }
             }
      }


Example of data in TXT file:

215865.wav
215864.wav
215875.wav
215887.wav
215885.wav
215884.wav
215902.wav
215906.wav


Attempted to run script above on some test data so it didn't have to scan through hundreds of folders...runs successfully but nothing gets deleted.
Your approach is very inefficient  


the array $txtfile is already in memory  
iterating it and per entry searching recursivly the directories (the var $dir in fact also includes files) creates a lot of unneccessary overhead.


$WorkingDir = 'D:\TEST\'
$txtFile = Get-Content "D:\TEST\Files.txt"

Get-ChildItem $WorkingDir -Recurse|ForEach-Object{
    If ($txtFile -contains $_.Name){
        Remove-Item $_.FullName -WhatIf
    }
}



If the output looks OK, remove the trailing -WhatIf