locked
Problem with Get-ChildItem when finding folders RRS feed

  • Question

  • Hi all,

    I have what I consider a very strange problem with Get-ChildItem

    My system:
    OS: Widows 10 Pro
    PSVersion: 5.1.15063.726
    PSEdition: Desktop

    My problem is that I want to find two specific directories in a specific directory, but not any subdirectories that may have the same name(s).

    Using "Documents" and "Downloads" as examples:
    Works: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory Documents
    Works: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory Downloads
    Doesn't Work: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory Downloads,Documents

    This I get, but what I don't get is:
    Works: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory -Include *
    Doesn't Work: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory -Include Documents
    Doesn't Work: Get-ChildItem -path "C:\Users\LarsPetersson" -Directory -Include *Documents*
    Doesn't Work: Get-ChildItem -path "C:\Users\LarsPetersson\*" -Directory -Include *Documents*
    Doesn't Work: Get-ChildItem -path "C:\Users\LarsPetersson" -File -Include github_RSA
    Works: Get-ChildItem -path "C:\Users\LarsPetersson\*" -File -Include github_RSA

    Or am I completely wrong and this shouldn't work, despite the Get-Help file seeming to indicate that it should?

    For now, I am using
    Get-ChildItem -path "C:\Users\LarsPetersson\*" -Directory | where {($_.psiscontainer -eq $true) -AND (($_.Name -eq "Documents") -OR ($_.Name -EQ "Downloads"))} | FT Fullname
    but this seems like a lot of typing that shouldn't be necessary.

    and yes, I know that "($_.psiscontainer -eq $true)" is superfluous here, but I don't feel I can trust Get-ChildItem to only show me directories like it is supposed to.
    Thursday, November 30, 2017 10:54 AM

Answers

  • Get-ChildItem C:\Users\vincent.k -Directory | Where-Object {$_.Name -match 'Documents$|Downloads$'}
    

    or

    'Documents','Downloads' | ForEach-Object {Get-ChildItem -Path C:\Users\vincent.k -Filter $_}


    Thursday, November 30, 2017 11:41 AM