none
PowerShell Script to Delete Files Based on Current Date RRS feed

  • Question

  • I know by using Get-Date PowerShell will look at the current date.  I would like to make a very basic PS Script which will delete a file older than 8 days based on the current day.  I believe using a if/then function will be necessary.  I am very new to scripting so Im not sure how the syntax is written.  All help appreciated.
    Thursday, December 4, 2014 7:48 PM

Answers

  • Yes, use -Recurse to get subfolders too:

    $Folder = "C:\Users\Me\Backups"
    Get-ChildItem -Path $Folder -Recurse | 
        Where { $_.LastWriteTime -lt [DateTime]::Today.AddDays(-8) } | 
            Sort LastWriteTime | 
                Remove-Item $_ -Force -Confirm:$false


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    • Marked as answer by itbanker2014 Thursday, December 4, 2014 8:35 PM
    Thursday, December 4, 2014 8:09 PM
  • Sam your wonderful demonstration is appreciated!  To conclude, by looking at what you have wrote I would think to place the whole path in the $Folder = ".\"   for example  $Folder = "C:\Users\Me\Backups"  , right?
    Also, this will be OK on entire folders hopefully...  specifying each item within would be a chore!

    • Marked as answer by itbanker2014 Thursday, December 4, 2014 8:35 PM
    Thursday, December 4, 2014 8:07 PM

All replies

  • $Folder = ".\"
    Get-ChildItem -Path $Folder | 
        Where { $_.LastWriteTime -lt [DateTime]::Today.AddDays(-8) } | 
            Sort LastWriteTime | 
                Remove-Item $_ -Force -Confirm:$false


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Thursday, December 4, 2014 8:03 PM
  • Sam your wonderful demonstration is appreciated!  To conclude, by looking at what you have wrote I would think to place the whole path in the $Folder = ".\"   for example  $Folder = "C:\Users\Me\Backups"  , right?
    Also, this will be OK on entire folders hopefully...  specifying each item within would be a chore!

    • Marked as answer by itbanker2014 Thursday, December 4, 2014 8:35 PM
    Thursday, December 4, 2014 8:07 PM
  • Yes, use -Recurse to get subfolders too:

    $Folder = "C:\Users\Me\Backups"
    Get-ChildItem -Path $Folder -Recurse | 
        Where { $_.LastWriteTime -lt [DateTime]::Today.AddDays(-8) } | 
            Sort LastWriteTime | 
                Remove-Item $_ -Force -Confirm:$false


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    • Marked as answer by itbanker2014 Thursday, December 4, 2014 8:35 PM
    Thursday, December 4, 2014 8:09 PM