List all Directories that do not contain a file with Wildcard Match

Question:

I need to be able to list all of the directories in a root folder that do not contain a file which contains the string merged. So for example, if I run the script from the directory C:\out, I will get a list of all the folders inside of C:\out which do not match the wildcard merged.

Example:

Tree:

Output:

Answer:

You can use Get-ChildItem, Where-Object, and Select-String:

Below is an explanation of what each line does:

  1. Get all directories in the current directory.
  2. Set up a filter that searches recursively through each directory for file names that contain the string merged. Only pass on the directory if none are found.
  3. Get the Name and LastWriteTime attributes of each directory.

Note too that you can write it more concisely if you want:

Source:

List all Directories that do not contain a file with Wildcard Match by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply