Filter a PowerShell file listing with Get-ChildItem
- Published: 2026.10.17
- Updated: 2026.10.16
- Windows
Before searching a collection of logs, it helps to list exactly which files are there. PowerShell’s Get-ChildItem can limit the location, file type and depth of the search without opening every file in an editor.
The examples use the Windows file system in Windows PowerShell 5.1 and PowerShell 7. Assume a folder named logs[demo] containing app.log and notes.txt, with old.log inside its archive subfolder.
List files with a name filter
Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' |
Select-Object Name, Length
Only app.log matches at the top level. -File restricts the result to files, and -Filter passes the name condition to the file-system provider. Length is the size in bytes and depends on the actual file content.
-LiteralPath keeps the brackets in the folder name literal. Separating the exact starting location from the file-name filter makes the command easier to understand and avoids interpreting the folder name as a wildcard pattern.
Include subfolders
Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' -Recurse |
Sort-Object FullName | Select-Object Name
Adding -Recurse includes old.log from archive as well as app.log. Start from the smallest relevant folder to avoid an unnecessarily broad scan. If different folders contain identical names, include FullName in the selected properties to tell the files apart.
Include hidden files
Get-ChildItem -LiteralPath '.\logs[demo]' -File -Force |
Sort-Object Name | Select-Object Name
On Windows, -Force includes files with the hidden attribute. It does not grant permission to read protected locations. An access-denied error means part of the requested scan may be unreadable; suppressing the error does not make the resulting list complete.
List folders instead
Get-ChildItem -LiteralPath '.\logs[demo]' -Directory |
Select-Object Name
For the sample layout, this returns archive. Choosing -Directory or -File at the source avoids filtering a mixed listing later.
Return relative names
Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' -Recurse -Name |
Sort-Object
app.log
archive\old.log
-Name returns strings rather than file-information objects. During recursion, these are names relative to the starting location. Keep the object result, without -Name, if later steps need properties such as size or the full path.
No matching files normally means no result objects. A missing starting path or an access error is a different outcome. Check those conditions before treating an empty listing as proof that no relevant logs exist. Once the list is correct, pass the chosen file paths to a content-search command such as Select-String.
References
-
Previous article
Join paths in PowerShell without confusing joining and existence checks 2026.10.16
-
Next article
Unstage a file in Git while keeping your edits 2026.10.17