How to use Depth pararmeter in Get-ChildItem PowerShell?


When –Depth parameter is used along with Get-ChildItem, it controls the recursion of displaying the subfolders and their contents. For example, if –Depth parameter is set to 1 then it will display only the content of the parent folder and immediate subfolders but not the subfolders of the subfolders.

Command

When you specify –Depth parameter, no need to add –Recursion parameter. It will automatically set the depth level.

Get-ChildItem D:\Temp\ -Depth 1

Output

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\ -Depth 1
    Directory: D:\Temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       13-12-2019     09:52                GPO_backup
d-----       24-11-2018     11:31                LGPO
-a----       07-05-2018     23:00            301 cars.xml
-a----       29-12-2017     15:16           4526 healthcheck.html
-a----       29-12-2017     15:16           4526 healthcheck1.html
-a----       08-12-2017     10:24          48362 servicereport.html
-a----       08-12-2017     10:24          48362 servicereport1.html
-a----       08-12-2017     10:16            393 style.css
-a----       08-12-2017     11:29           7974 Test.xlsx
-a----       25-10-2017     08:13            104 testcsv.csv
-a----       12-12-2017     23:04           1034 testhtmoutput.html
    Directory: D:\Temp\GPO_backup
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24-11-2018     11:34                {C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}
    Directory: D:\Temp\LGPO
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       01-06-2017     03:22         410088 LGPO.exe
-a----       01-06-2017     02:25         638115 LGPO.pdf

In the above example, -Depth value is set to 1, so it displays only its subfolders (D:\Temp\LGPO, D:\Temp\GPO_Backup) and their contents.

When you set the –Depth value to 2, the output will be.

Output

Directory: D:\Temp\GPO_backup
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24-11-2018     11:34                {C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}
    Directory: D:\Temp\GPO_backup\{C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24-11-2018     11:34                DomainSysvol
-a----       24-11-2018     11:34           6215 Backup.xml
-a----       24-11-2018     11:34            602 Bkupinfo.xml
    Directory: D:\Temp\LGPO
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       01-06-2017     03:22         410088 LGPO.exe
-a----       01-06-2017     02:25         638115 LGPO.pdf

As you can see in above example, additional level (D:\Temp\GPO_backup\{C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}) has been added.

Updated on: 23-Jan-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements