Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit Get-ChildItem recursion depth

Tags:

powershell

I can get all sub-items recursively using this command:

Get-ChildItem -recurse 

But is there a way to limit the depth? If I only want to recurse one or two levels down for example?

like image 868
Svish Avatar asked Nov 06 '12 10:11

Svish


People also ask

Is Get-ChildItem recursive?

By default, the Get-ChildItem cmdlet displays symbolic links to directories found during recursion, but doesn't recurse into them. Use the FollowSymlink parameter to search the directories that target those symbolic links. The FollowSymlink is a dynamic parameter and is supported only in the FileSystem provider.

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.

How do you get the full path of ChildItem?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

What does recurse mean in PowerShell?

-Recurse is a classic switch, which instructs PowerShell commands such as Get-ChildItem to repeat in sub directories. Once you remember that -Recurse comes directly after the directory, then it will serve you well in scripts that need to drill down to find information.


1 Answers

Use this to limit the depth to 2:

Get-ChildItem \*\*\*,\*\*,\* 

The way it works is that it returns the children at each depth 2,1 and 0.


Explanation:

This command

Get-ChildItem \*\*\* 

returns all items with a depth of two subfolders. Adding \* adds an additional subfolder to search in.

In line with the OP question, to limit a recursive search using get-childitem you are required to specify all the depths that can be searched.

like image 189
CB. Avatar answered Oct 23 '22 17:10

CB.