Home » PowerShell Tips » PowerShell – Get-ChildItem Get folders Only

PowerShell – Get-ChildItem Get folders Only

In this article, I will explain you how to get folders only using PowerShell Get-ChildItem. using Get-ChildItem, we can do more tasks related to get folder name, get folder size or get empty folder and so many.

Let’s understand PowerShell Get-ChildItem cmdlet to get folder and its related properties using examples.

Get-ChildItem Get Folders Only

To get folders only excludes files, use PowerShell Get-ChildItem -Directory attribute to display folders is on PowerShell console.

Get-ChildItem -Path D:\PowerShell\ -Directory  

Output of the above Get-ChildItem command with path specified by –Path parameter and Directory parameter list folders only as below

    Directory: D:\PowerShell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24-10-2020     19:17                Excel
d-----       17-07-2021     15:15                FTP-101
d-----       22-07-2021     23:23                FTP-102
d-----       05-10-2020     21:14                Zip

Get Folders Recursively

Use PowerShell Get-ChildItem Recurse parameter to get folder only recursively. It gets folder from container and child containers.

Get-ChildItem -Path D:\PowerShell\ -Directory -Recurse  

Above PowerShell command, get folders recursively using Recurse parameter and list folders on PowerShell console.

Cool Tip: Learn how to get current directory path in PowerShell!

Get-ChildItem Empty Folders Only

Using PowerShell Get-ChildItem cmdlet to get empty folders if file count in folder is equal to 0.

Get-ChildItem -Path D:\PowerShell -Recurse -Directory | Where-Object {$_.GetFiles().Count -eq 0}

Above PowerShell command, recursively search for directory using –Recurse parameter and pass it object to another command to search for file count.

Second command check for directory files count equal to 0 and return empty folders only.

Get-ChildItem Get Folder Name Only

To get folder name only in PowerShell, use Get-ChildItem –Directory parameter and select Name property to list folder name only on PowerShell console.

Get-ChildItem -Path D:\PowerShell -Directory | select Name 

Above PowerShell command, get directory name using Name property.

Cool Tip: How to find large size files in PowerShell!

Get-ChildItem Get System Folder Only

To get system folder only in PowerShell, use Get-ChildItem –System parameter

Get-ChildItem -Path C:\ -Directory -System -Recurse

Above PowerShell command get system folder only. If you don’t have administrator privilege, it will throw exception message as access to folder path is denied.

Cool Tip: Learn how to use multiline command in PowerShell!

Get-ChildItem Get Directory Count

To get directory count inside parent folder, use Get-ChildItem cmdlet to get all directory specified by -Path parameter and use Measure-Object to get directory count.

Get-ChildItem -Path D:\PowerShell\ -Directory| Measure-Object

In the above PowerShell command, it gets all directory and pass through Measure-Object to get directory count.

Cool Tip: Learn how to get permission on folders and subfolders in PowerShell!

Conclusion

I hope above article helps you to understand PowerShell Get-ChildItem cmdlet to get folders only or folder related properties.