locked
How to use 'Get-ChildItem' cmdlet to read files and folders but exclude some directories. RRS feed

  • Question

  • Hi everyone!

      Is there a way to use 'Get-ChildItem' cmdlet to read files and folders but exclude some directories?

    Hope I was clear enough.


    Doria

    Wednesday, February 20, 2019 12:34 PM

Answers

  • Here:

    Where-Object

    btw, new code version:

    Get-ChildItem -Path $path -File -Recurse |  Where-Object {($_.Directory -CNotLike "*$excludepath*" -and $_.Length -gt 0)}


    Doria

    • Marked as answer by dydoria Monday, February 25, 2019 5:17 PM
    Wednesday, February 20, 2019 2:26 PM

All replies

  • How about the parameter -Exclude? Did you read the help?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, February 20, 2019 12:47 PM
  • It is not for directories.... but the following did the job!

    Where {($_.Directory -notlike $excludepath -and $_.Length -gt 0)}

    Thanks for your answer.


    Doria

    • Proposed as answer by Naw Wednesday, February 20, 2019 4:21 PM
    Wednesday, February 20, 2019 1:10 PM
  • It is not for directories....

    Why do you think that? Where did you read that?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, February 20, 2019 1:16 PM
  • Here:

    Where-Object

    btw, new code version:

    Get-ChildItem -Path $path -File -Recurse |  Where-Object {($_.Directory -CNotLike "*$excludepath*" -and $_.Length -gt 0)}


    Doria

    • Marked as answer by dydoria Monday, February 25, 2019 5:17 PM
    Wednesday, February 20, 2019 2:26 PM
  • How about the parameter -Exclude? Did you read the help?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    -exclude seems to only apply to the leaf of the path...  https://github.com/MicrosoftDocs/PowerShell-Docs/issues/3449

    Wednesday, February 20, 2019 3:08 PM
  • I was aware of that but the OP did not ask about subdirectories actually.  ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, February 20, 2019 4:00 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, February 25, 2019 7:16 AM