Powershell just dates from Get-Date RRS feed

  • Question

  • I'm trying to get a list of files that is 30 days old. But when I execute the code below, I get anything before the time I run it. For example, if I run it at 5:00pm, it'll not pick up the files created at 5:01pm from a month ago.

    $dow= (Get-Date).dayofweek
    $deldate= (Get-Date).AddDays(-30)
    $fullbkupdir = "\\server\location\"+$dow
    Get-ChildItem -Path $fullbkupdir -Recurse | Where {$_.CreationTime -lt $deldate}

    How do I get PS to pick up all files from 12:01am - 11:59pm that is 30 days old?

    Thank you

    • Edited by Ami2013 Friday, March 2, 2018 9:15 PM
    • Moved by Bill_Stewart Monday, May 7, 2018 9:59 PM This is not "explain basic logic to me" forum
    Friday, March 2, 2018 9:12 PM

All replies

  • That is what the code does.  Anything older than 30 days ago will be returned.

    If you want only 30 days ago then we would do this.

    Get-ChildItem -Path $fullbkupdir -Recurse | Where{$_.CreationTime.Date -eq [datetime]::Today.AddDays(-30)}

    That is 30 days BEFORE today. FOr 30 days less than "Now"

    Get-ChildItem -Path $fullbkupdir -Recurse | Where{$_.CreationTime.Date -eq [datetime]::Now.AddDays(-30).Date}

    It is all about boundaries and adjusting the date to match the time field always.


    Friday, March 2, 2018 9:26 PM