December 21st, 2016

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

Doctor Scripto
Scripter

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.