Kevin Missildine
Kevin Missildine

Reputation: 25

PowerShell Script finding File using extension and excluding a folder

I am using the below code:

Get-ChildItem -Path N:\USERS -Filter DANTOM.DTM -Recurse -ErrorAction SilentlyContinue -Force

I need it to either find the file "DANTOM.DTM" or the extension ".DTM". I need to exclude the folder N:\USERS\EDI because it is a 1.7TB folder that would never have this file in it. So in doing so would really speed up the process.

I would like the end result to either spit into a .txt file saying which folders inside of N:\USERS has the file or just have it display as a list in powershell.

Thank you,

Upvotes: 1

Views: 72

Answers (1)

mklement0
mklement0

Reputation: 440689

Assuming that the files of interest do not reside directly in N:\USERS (only in subdirs.), try the following (PSv3+ syntax); send to a file by appending > dirs.txt, for instance.

Get-ChildItem N:\USERS -Directory | ? Name -ne 'EDI' | 
  Get-ChildItem -Recurse -Filter *.DTM | 
    ForEach-Object { $_.DirectoryName }

Note: While it is tempting to try a simpler approach with -Exclude EDI, it unfortunately doesn't seem to be effective in excluding the entire subtree of the EDI subfolder.

Upvotes: 1

Related Questions