How to get only folders but not the files in the Get-ChildItem in PowerShell?



To get only folders excluding files, we need to use –Attribute parameter with Directory attribute.

Command

Get-ChildItem D:\Temp\ -Attributes Directory

Output

Directory: D:\Temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       13-12-2019     09:52                GPO_backup
d-----       24-11-2018     11:31                LGPO

Similarly, you can combine different operations.

To get the only system directories, use the below command.

Get-ChildItem D:\Temp\ -Attributes Directory –System -Recurse

To get the system directories that are hidden.

Get-ChildItem D:\Temp\ -Attributes Directory -Recurse –System -Hidden

To get the system directories which are Readonly.

Get-ChildItem D:\Temp\ -Attributes Directory -Recurse –System -Readonly

Advertisements