Issue with PowerShell Get-ChildItem not returning expected results in a directory

PAPPY 20 Reputation points
2024-08-30T02:37:03.2033333+00:00

I'm trying to list all .txt files in a specific directory using PowerShell on a Windows Server. I don't want to search recursively, but I'm only getting files from the root of the directory and none from subdirectories. Here’s the command I'm using:

powershellCopy code
Get-ChildItem -Path 'C:\Logs' -Filter '*.txt'

1x1

Am I missing something, or is there a way to include files from the first level of subdirectories

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,513 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 36,336 Reputation points Microsoft Vendor
    2024-08-30T07:57:45.6166667+00:00

    Hi,

    You can set the number of subdirectory levels using the Depth parameter.

    Get-ChildItem -Path 'C:\Logs' -Filter '*.txt' -Depth 1
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.4#-depth

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.