Search code examples
powershellcmddirget-childitem

Get-ChildItem: Show only folders that do not include a specific file


First post on the platform ;-)

Scenario: I have a root folder that contains subfolders with setup related files. Some subfolders contain more subfolders with more files, subsubfolders. Each of the subfolders underneath the root folder, or their subfolders, MUST include one specific file.

Now I want to get all subfolders directly located underneath the root folder THAT DO NOT INCLUDE this specific file, neither in the subfolder itself, nor in other subfolders below. I want only the names of the subfolders underneath the root. The search should be recursevely, of course.

My current code looks like that:

Get-ChildItem $softwarePath -Recurse | ? Name -notlike "Prefix_*.cmd"

Unfortunately the result is very huge and it includes all files in every subfolder, too. My wish: Only get the names of the subfolders underneath the root to check, wy that specific file is not there.


Solution

  • Welcome to stackoverflow!

    You are almost there. Here a solution where I first retrieve all subfolders of $softwarePath and then select only the one which not contains the file:

    Get-ChildItem $softwarePath -Directory | 
    ? { -not (Get-ChildItem $_.FullName -Recurse -File -Filter 'Prefix_*.cmd')}