In one of my recent tasks, I have to deal with files and folder-based operations on a local system. File or folder-based operation is a very cumbersome process using a manual way if you have a lot many files or directories to look for.
Using PowerShell, it’s very easy to list files in a directory or list folders based on filter criteria.
PowerShell PSIsContainer to list of files in a 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 the root directory of the file system.
Using PowerShell PSIsContainer, it’s very easy to get files in the directory only or select folders only.
In this article, I will explain using PowerShell PSIsContainer with different examples to list files in the directory or list directory, or special folders only.
PowerShell PSIsContainer to List Folders and Subfolders
PowerShell Get-ChildItem cmdlet gets file and folders only.
To get a list of folders and subfolders in the filesystem, use a filter based on a folder that has 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, and folders recursively and passes its output to another command.
In the second command, it checks for the folder which has the PowerShell PSIsContainer property set to $true and gets a list of folders and subfolders in the file system.
Cool Tip: Get-ChildItem cmdlet – Search for files in PowerShell!
PowerShell List Files in Directory
To get a list of files in a directory, use a filter based on a file that has the PowerShell PSIsContainer property set to $false
.
Use the below command to list all files in directory and subdirectories.
PS C:\> Get-ChildItem -Path D:\PowerShell\Excel\ -Recurse | Where-Object {$_.PSIsContainer -eq $false}
The above PowerShell script, using the PowerShell Get-ChildItem cmdlet, it returns files and folders.
It passes its output to another command where it checks 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
PowerShell Tip: How to find files with lastwritetime in PowerShell!
The 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 them, use PowerShell PSIsContainer uses the property of all file system object to select folders only which has the property set to true.
Use 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 their output to another command.
The second command check PowerShell PSIsContainer property is true to filter only the directory and pass its output to the next command
The third command, get files count for each folder and check if it is equal to 0 and returns folders having no files.
Cool Tip: How to fix the PowerShell script is not digitally signed!
Conclusion
Use the PowerShell PSIsContainer property of the file system object to select files if the property is set to $false in their PSIsContainer property and select folder if the property is set to $true in their PSIsContainer property.
Using PowerShell Get-ChildItem
cmdlet and PSIsContainer
to list files in the directory or list all files in the directory and subdirectories.
Cool Tip: How to list files sorted by date in PowerShell!
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.