PowerShell – List Files in Directory

In one of my recent task, I have to deal with files and folder based operation on local system. Doing files or folders based operation is very cumbersome process using manual way if you have lot many files or directory to look for. Using PowerShell, its very easy to get list files in directory or list folders based on filter criteria.

PowerShell PSIsContainer to get list of files in directory which has PSIsContainer property set to $false in their file system object. Folder or Directory has PSIsContainer property set to $true in their container.

PowerShell Get-ChildItem cmdlet returns files or folders in root directory of file system. Using PowerShell PSIsContainer, its very easy to get files in directory only or select folders only.

In this article, I will explain using PowerShell PSIsContainer with different examples to list files in directory or list directory or special folders only.

PowerShell PSIsContainer to List Folders and Subfolders

PowerShell Get-ChildItem cmdlet returns file and folder only. To get list of folders and subfolders in filesystem, use filter based on folder that have PSIsContainer property set to $true

PS C:\> Get-ChildItem -Path D:\PowerShell -Recurse | Where-Object {$_.PSIsContainer} 

In the above PowerShell script, Get-ChildItem returns files, folders recursively and pass its output to another command.

In second command, it check for folder which has PowerShell PSIsContainer property set to $true and get list of folders and subfolders in file system.

Cool Tip: Get-ChildItem cmdlet – Search for files in PowerShell!

PowerShell List Files in Directory

To get list of files in directory, use a filter based on file which has PowerShell PSIsContainer property set to $false.

Use below command to list all files in directory and subdirectories.

PS C:\> Get-ChildItem -Path D:\PowerShell\Excel\ -Recurse | Where-Object {$_.PSIsContainer -eq $false}  

In the above PowerShell script, using PowerShell Get-ChildItem cmdlet, it returns files and folders.

It pass its output to another command where it check for PowerShell PSIsContainer property is equal to false to filter files only.

It returns list files in directory and subdirectories, it prints files with their Mode, LastWriteTime, File size and File Name

Output of the above command

PS C:\> Get-ChildItem -Path D:\PowerShell\Excel\ -Recurse | Where-Object {$_.PSIsContainer -eq $false}                                                                                       

    Directory: D:\PowerShell\Excel


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       05-10-2020     21:14            559 DownloadZipFile-Unzip.ps1
-a----       08-10-2020     09:40            105 import-csv.ps1
-a----       04-10-2020     17:06            199 IsModuleInstalled.ps1
-a----       24-10-2020     20:08            598 ping-computers.ps1
-a----       10-10-2020     19:37            139 quser.ps1
-a----       04-10-2020     17:05            911 rename-adgroup.ps1
-a----       04-10-2020     13:22            861 search-excel.ps1
-a----       06-10-2020     19:06             25 throw.ps1
-a----       06-10-2020     18:09            182 Unzip-file.ps1


PS C:\>  

Cool Tip: To get memory usage in PowerShell!

PowerShell PSIsContainer to Get Folders with No Files

To get folders and subfolders having no files in it, use PowerShell PSIsContainer uses property of all file system object to select folders only which has property set to true. Using GetFiles().Count property to get file count.

PS C:\> Get-ChildItem -Path D:\PowerShell -Recurse | Where-Object {$_.PSIsContainer -eq $true} | Where-Object {$_.GetFiles().Count -eq 0} 

In the above PowerShell script, Get-ChildItem cmdlets return files and folders recursively and pass it output to another command.

The second command check PowerShell PSIsContainer property is true to filter only directory and pass it output to next command

The third command, get files count for each folder and check if it equal to 0 and returns folders having no files.

Cool Tip: How to fix PowerShell script is not digitally signed!

Conclusion

Using PowerShell, you can perform task very easily. Use PowerShell PSIsContainer property of file system object to select files if property set to $false in their PSIsContainer property and select folder if property set to $true in their PSIsContainer property.

Using PowerShell Get-ChildItem cmdlet and PSIsContainer to list file in directory or list all files in directory and subdirectories.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.

Leave a Comment