PowerTip: Limit Get-Childitem to a limited depth in the tree

The Scripting Guys

Dr Scripto

Summary: Use the new -depth parameter to control how far down a directory you can search.

Hey, Scripting Guy! Question I have a large file structure to search but I only want to go two folders deep on the search.  Is there a way to do this in PowerShell?

Hey, Scripting Guy! Answer Certainly!  In PowerShell 5.0, you can use the -depth parameter to specify how many branches deep you’d like to limit from the search root. Here, we can search the C:\Users\CurlyBlue folder for DOCX files but only go to two levels below. Here is an example:

Get-Childitem C:\Users\CurlyBlue -include *.docx -depth 2

The Doctor

The Scripting Guys
Dr Scripto

Follow Dr Scripto   

No Comments.