Powershell – IO.Directory – Find file types in all subdirectories

Question:

I ran across this code in another post that almost does what I need, but can’t figure out how to modify it to look for specific file types, i.e. *.bak, .txt, etc. I’m using Powershell with [System.IO.DirectoryInfo] because, like others have stated, using Get-ChildItem is too slow across the network. I thought it would just be something like $fileEntries = [IO.Directory]::GetFiles(“C:\”, “.bak”), but it still returns every file in every directory. –PS/.NET newbie

Answer:

This will loop through each extension searching for all files in the root and sub-directories. Ensure you have the correct privileges on all the directories especially when you’re running from C:\ as the root.

This method will only work with Powershell running .Net 4.0 or higher.
To check and update the version of .Net:

The CLRVersion value is the .net version.

Update the config file as follows:

Restart the Powershell session and verify the CLRVersion value in the $PSVersionTable variable.

Source:

Powershell – IO.Directory – Find file types in all subdirectories by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply