locked
Get-Childitem extension RRS feed

  • General discussion

  • Hi all

    I have this line: $items =Get-Childitem $Path | Where {} 

    What I need is get the files that are .zip any idea?

    Thnks

    Thursday, March 3, 2016 9:34 PM

All replies

  • Get-Childitem $Path | Where {$_.extension -like ".zip"}

    or Get-Childitem $Path -filter "*.zip"

    Thursday, March 3, 2016 10:02 PM
  • Get-Childitem $Path -filter "*.zip"
    Friday, August 31, 2018 8:59 PM
  • No quotes:

    Get-Childitem $Path -Include *.zip


    \_(ツ)_/

    Friday, August 31, 2018 9:21 PM
  • $items = get-childitem | where name -like *.zip
    $items = get-childitem $path\*.zip

    -recurse works with these too


    • Edited by JS2010 Saturday, September 1, 2018 3:20 PM
    Saturday, September 1, 2018 2:44 PM
  • No quotes:

    Get-Childitem $Path -Include *.zip


    \_(ツ)_/

    That only works if you include a wilcard (e.g. c:\temp\*) in the -Path $Path Variable. Else you are forced to use -Recurse for this to work.

    I prefer using the Filter parameter, because filters are applied when retrieving the objects, which is more efficient than having PowerShell filter the objects after they are retrieved.

    None the less, even though you don't explicitly need to specify the Path parameter, I prefer to clarify it. 

    Get-ChildItem -Path $Path -Filter *.zip

    Saturday, September 1, 2018 8:54 PM
  • No quotes:

    Get-Childitem $Path -Include *.zip


    \_(ツ)_/

    That only works if you include a wilcard (e.g. c:\temp\*) in the -Path $Path Variable. Else you are forced to use -Recurse for this to work.

    I prefer using the Filter parameter, because filters are applied when retrieving the objects, which is more efficient than having PowerShell filter the objects after they are retrieved.

    None the less, even though you don't explicitly need to specify the Path parameter, I prefer to clarify it. 

    Get-ChildItem -Path $Path -Filter *.zip

    Correct but...The point I was making was that "Filter" will not work with all providers and all file system sources so beware.


    \_(ツ)_/

    Saturday, September 1, 2018 9:21 PM
  • Note that -include only works with -recurse or a wildcard in the path. (actually it works all the time in PS 6)

    Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path.

    -include and -literalpath also seem problematic in PS 5.

    Sunday, September 2, 2018 2:01 PM
  • Hmm, looks like -filter is faster.

    PS C:\windows> measure-command { ls -r -filter *.exe -ea 0 }


    Days              : 0
    Hours             : 0
    Minutes           : 0
    Seconds           : 5
    Milliseconds      : 617
    Ticks             : 56179874
    TotalDays         : 6.50230023148148E-05
    TotalHours        : 0.00156055205555556
    TotalMinutes      : 0.0936331233333333
    TotalSeconds      : 5.6179874
    TotalMilliseconds : 5617.9874



    PS C:\windows> measure-command { ls -r -include *.exe -ea 0 }


    Days              : 0
    Hours             : 0
    Minutes           : 0
    Seconds           : 18
    Milliseconds      : 766
    Ticks             : 187664745
    TotalDays         : 0.000217204565972222
    TotalHours        : 0.00521290958333333
    TotalMinutes      : 0.312774575
    TotalSeconds      : 18.7664745
    TotalMilliseconds : 18766.4745

    Monday, September 3, 2018 3:15 AM