locked
Get-ChildItem FileSystem: The -Exclude param works fine on normal folders but not on root drives? RRS feed

  • Question

  • The behavior I'm seeing is that if my working directory is NOT a root drive (e.g. C:\, D:\, etc.), "gci -Exclude "someFolder" works fine.  However, if my working directory is a root drive, a flat call to gci works fine but when I use the -Exclude param on even one file/folder, the output displays nothing.  I've tested this on multiple drives in Windows 7 and Windows 2008.  Can anyone explain this behavior?  Thanks.

    Edit:

    PS C:\temp>gci  <--- works
    PS C:\temp>gci -Exclude "someFolder" <--- works
    PS C:\gci <--- works
    PS C:\gci -Exclude "someFolder" <--- get 0 results...???

    • Edited by thepip3r Monday, October 17, 2011 8:11 PM
    Monday, October 17, 2011 8:09 PM

Answers

  • The following fails:

    gci -path c:\ -exclude windows

    but the following lists files in the root and in all subfolders except the one excluded:

    gci -path c:\* -exclude windows

     


    Richard Mueller - MVP Directory Services
    • Proposed as answer by Bigteddy Friday, October 21, 2011 5:12 PM
    • Marked as answer by thepip3r Friday, October 21, 2011 5:55 PM
    Friday, October 21, 2011 5:07 PM

All replies

  • The behavior I'm seeing is that if my working directory is NOT a root drive (e.g. C:\, D:\, etc.), "gci -Exclude "someFolder" works fine.  However, if my working directory is a root drive, a flat call to gci works fine but when I use the -Exclude param on even one file/folder, the output displays nothing.  I've tested this on multiple drives in Windows 7 and Windows 2008.  Can anyone explain this behavior?  Thanks.

    Edit:

    PS C:\temp>gci  <--- works
    PS C:\temp>gci -Exclude "someFolder" <--- works
    PS C:\gci <--- works
    PS C:\gci -Exclude "someFolder" <--- get 0 results...???

    -exclude \somefolder.

    Without the slash the folder is seen as a relative path.  A path reltive to th e current folder and not to th eroor.  The slash says 'at the root'

    .Note that you may really want to use .\somefolder

     

     


    jv
    • Edited by jrv Monday, October 17, 2011 8:42 PM
    Monday, October 17, 2011 8:41 PM
  • Also note that this will exclude any object with 'test' anywhere in its name:

    dir -exclude *test* -recurse


    jv
    Monday, October 17, 2011 8:52 PM
  • I tried:

    PS C:\>gci -exclude ".\someFolder"
    PS C:\>gci -exclude .\someFolder
    PS C:\>gci -exclude \someFolder
    PS C:\>gci -exclude "\someFolder"
    PS C:\>gci -exclude "someFolder"
    PS C:\>gci -exclude someFolder

    ...and same results -- no results.

     

    Are you actually getting results using one of the above?

    Monday, October 17, 2011 8:57 PM
  • I tried:

    PS C:\>gci -exclude ".\someFolder"
    PS C:\>gci -exclude .\someFolder
    PS C:\>gci -exclude \someFolder
    PS C:\>gci -exclude "\someFolder"
    PS C:\>gci -exclude "someFolder"
    PS C:\>gci -exclude someFolder

    ...and same results -- no results.

     

    Are you actually getting results using one of the above?


    Yes

    dir -exclude test -recurse will exclude an item named test.

     


    jv
    Monday, October 17, 2011 10:12 PM
  • yes, yes... i'm aware that adding the -recurse parameter "fixes" the problem but it doesn't explain the differing behavior with the command run at the root level of a drive.
    Tuesday, October 18, 2011 3:09 AM
  • yes, yes... i'm aware that adding the -recurse parameter "fixes" the problem but it doesn't explain the differing behavior with the command run at the root level of a drive.


    I don't see the problem.  The command works the same at the root or elsewhere.

     


    jv
    Tuesday, October 18, 2011 3:20 AM
  • I agree with thepip3r.  gci does not produce the expected results.  Only when used in conjunction with -Recurse do the -Include and -Exclude parameters do anything useful.

    FWIW, -Filter works as expected without -Recurse, but it will only "include", not "exclude".


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"

    • Edited by Bigteddy Tuesday, October 18, 2011 5:52 AM
    Tuesday, October 18, 2011 5:34 AM
  • Also, notice this inconsistent behaviour.  From my scripts directory, I get the expected results.  From the root directory, I get nothing:

    cd C:\scripts
    gci -Directory -Exclude old
    # Returns all directories except the one named "old"
    cd \
    gci -Directory -Exclude Windows
    # Returns nothing
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, October 18, 2011 5:49 AM
  • Thanks for the confirmation Bigteddy -- I wasn't sure if a security requirement in my environment might be causing the situation or not but it's nice that someone else is experiencing the same thing... if anyone can explain this behavior, I'd greatly appreciate it.
    Tuesday, October 18, 2011 11:13 AM
  • After a little mucking around I see what yo are seeing.

     

    I think we can call that 'root rot'.  It seems to muck up everything at the root not just exclude.

     


    jv
    Tuesday, October 18, 2011 1:49 PM
  • I can confirm the same strange behavior on Windows 7 and Windows Server 2008 R2, both with PowerShell V2.0. I also see the problem on Windows Server 2003 and Windows XP with PowerShell V1.0, but I get an error message about invalid path. I tried many variations specifying -Path and -Exclude, but all raised the same error if you specify the root of the drive for the path.

     


    Richard Mueller - MVP Directory Services
    Friday, October 21, 2011 4:40 PM
  • Help Get-ChildIterm -para include

    -Include <string[]>
        Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

        The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

    Is it possible that this is also true of 'Exclude'?


    jv
    Friday, October 21, 2011 4:47 PM
  • The following fails:

    gci -path c:\ -exclude windows

    but the following lists files in the root and in all subfolders except the one excluded:

    gci -path c:\* -exclude windows

     


    Richard Mueller - MVP Directory Services
    • Proposed as answer by Bigteddy Friday, October 21, 2011 5:12 PM
    • Marked as answer by thepip3r Friday, October 21, 2011 5:55 PM
    Friday, October 21, 2011 5:07 PM
  • And this works as expected:
    gci -path c:\* -exclude windows,'program files',scripts,docum*

    The issue seems to be the need to include the contents (*) of the folders in order to get the 'exclude' parameter to actually be applied... I wonder why?


    jv
    • Edited by jrv Friday, October 21, 2011 5:22 PM
    Friday, October 21, 2011 5:12 PM
  • well if nothing else gents, thanks for the work-around.
    Friday, October 21, 2011 5:50 PM
  • The issue seems to be the need to include the contents (*) of the folders in order to get the 'exclude' parameter to actually be applied... I wonder why?

    Because something has to be there first before it can be excluded.

    Bill

    Friday, October 21, 2011 7:47 PM
  • The issue seems to be the need to include the contents (*) of the folders in order to get the 'exclude' parameter to actually be applied... I wonder why?

    Because something has to be there first before it can be excluded.

    Bill

    That is waht I thought but the folder is there even without the wildcard.  I thik the issue is more subtel.  The PosH team could tell us how they designed this but is it is not really obvious form looking at the behaviour.   It all seems counterintuitive.

     


    jv
    Friday, October 21, 2011 8:23 PM