locked
Why doesn't this work? (Get-Childitem -Filter) RRS feed

  • Question

  • Why doesn't this work? Result is empty.

    PS C:\> gci -Filter {LastWriteTime -le (Get-Date)}

    I do not want to use Where-Object to filter.

    Tuesday, May 22, 2018 9:00 PM

Answers

  • LastWriteTime is not a string it is a DateTime object.

    GCI does not support filters when used with the file system provider.

    gci . | ? {$_.LastWriteTime.Date -le [datetime]::Today.AddDays(-30)}

    gci . | ? {$_.LastWriteTime.Date -le [datetime]'5/1/2018'}

    "ParseExact" is never required here.


    \_(ツ)_/

    • Marked as answer by Xan Xan Wednesday, May 23, 2018 2:06 PM
    Tuesday, May 22, 2018 9:20 PM

All replies

  • Hi,

    The issue here is that $_.LastWriteTime is of Type DateTime and you are comparing it with string.

    You will need to create the DateTime object yourself, like:

    [datetime]::ParseExact("31/12/1900", "dd/MM/yyyy", $null)


    You may also convert the $_.LastWriteTime to string, like:

    $_.LastWriteTime.ToShortTimeString()



    Remember that the results will be different on computers with different culture settings.

    Complete example looks like this:

    gci . | ? {$_.LastWriteTime.Date -eq [datetime]::ParseExact("05/22/2018", "dd/MM/yyyy", $null)}

    Hope this helps!

    Kind regards,
    Leon


    Blog: https://thesystemcenterblog.com  LinkedIn:   

    Tuesday, May 22, 2018 9:13 PM
  • LastWriteTime is not a string it is a DateTime object.

    GCI does not support filters when used with the file system provider.

    gci . | ? {$_.LastWriteTime.Date -le [datetime]::Today.AddDays(-30)}

    gci . | ? {$_.LastWriteTime.Date -le [datetime]'5/1/2018'}

    "ParseExact" is never required here.


    \_(ツ)_/

    • Marked as answer by Xan Xan Wednesday, May 23, 2018 2:06 PM
    Tuesday, May 22, 2018 9:20 PM
  • I do not want to use Where-Object to filter.

    Why not? That's what it's for.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 22, 2018 9:49 PM
  • GCI does not support filters when used with the file system provider.

    Lame.
    Wednesday, May 23, 2018 2:06 PM
  • I do not want to use Where-Object to filter.

    Why not? That's what it's for.


    -- Bill Stewart [Bill_Stewart]


    So should -Filter be.
    Wednesday, May 23, 2018 2:07 PM
  • Unfortunately, wishful thinking does not cause features to spring into existence.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 23, 2018 2:18 PM
  • GCI does not support filters when used with the file system provider.

    Lame.

    What is "Lame"?

    The documentation clearly sates that the filter is not implemented for some providers.  It is useful in some that implement it.  With the FileSystem Provider the filter defaults to only file names and wildcards exactly as used in the "Incudes" and "Exclude" parameters.

    The "Where" CmdLet is a filter and is provided for cases like this.


    \_(ツ)_/

    Wednesday, May 23, 2018 2:56 PM