Use PowerShell to delete (or modify) files that were created (or modified) n days ago or on a specified date (before or after) _powershell

Source: Internet
Author: User

Originally want to use batch processing, think of time is too troublesome ...

Immediately install PowerShell look at the help document, which has an example:

The following command looks up the last modified date in the Program Files folder later than October 1, 2005 and neither
All executables less than 1 MB and less than MB (test found unable to run-_-!) :

Get-childitem-path $env:P rogramfiles-recurse-include *.exe | Where-object '
-filterscript {($_. Lastwritetime-gt "2005-10-01")-and ($_. Length-ge 1m) '
-and ($_. Length-le 10m)}

Changed to be the following, to delete the D:\test and subdirectories created 10 days ago as an example, test please be careful!
Because the content is too long to appear as multiple lines, it is actually a row. Use the "'" character as a continuation character (double quotation marks, is a heavy
Notes are not single quotes, equivalent to the "_" of the VBS, which tells Windows PowerShell that the next line is a continuation section,
It is useful in the case where the entire line cannot be placed in the library without wrapping. Only allow expressions to be the first of the pipeline
Elements.

One-line command gets the list of expired files:
Get-childitem-path D:\test-recurse-erroraction:silentlycontinue | `
Where-object-filterscript {((get-date)-($_. CreationTime)). DAYS-GT 10 '
-and $_. Psiscontainer-ne $True)} | Select-object FullName

One-line command to delete expired files:
Get-childitem-path D:\test-recurse-erroraction:silentlycontinue | `
Where-object-filterscript {((get-date)-($_. CreationTime)). DAYS-GT 10 '
-and $_. Psiscontainer-ne $True)} | Remove-item

One-line command to delete expired files (including deleting read-only, hidden):
Get-childitem-path D:\test-force-recurse-erroraction:silentlycontinue | `
Where-object-filterscript {((get-date)-($_. CreationTime)). DAYS-GT 10 '
-and $_. Psiscontainer-ne $True)} | Remove-item-force
Of course, you can use the alias shorthand command.


Or find System.IO.FileInfo in the Types.ps1xml file to increase the age member:

<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>Age</Name>
<GetScriptBlock>
((get-date)-($this. creationtime)). Days
</GetScriptBlock>
</ScriptProperty>
</Members>

Add content from <ScriptProperty> to </scriptproperty>, and do not add after modification.

Script content:

ForEach ($file in Get-childitem d:\test\*-force-recurse '
-erroraction:silentlycontinue)
  {
    if ($file). Age-ge 10-and $file. Psiscontainer-ne $True)
      {$file. Delete ()}
  }

{Remove-item-force "$file"} cannot be used here

The script extension is. PS1, in which the number 1 is the name of the extension.

-GT is greater than,-ge is greater than or equal to, others look help.

If the Psiscontainer property is true, it means that the folder is processed rather than the file.

-force is including read-only, hidden and other system files, it is best to use the-erroraction.

The-erroraction:silentlycontinue function is to continue executing scripts without displaying errors, such as when recursion encounters
The System Volume information, and so on, have no access to an error.

Find files created before a specified date:

Get-childitem-path D:\test-force-recurse-erroraction:silentlycontinue | '
where-object-filterscript {$_. Creationtime-gt "2011-01-01")-and '
($_. Psiscontainer-ne $True)} | Select-object FullName

Find files modified before a specified date:

Get-childitem-path D:\test-force-recurse-erroraction:silentlycontinue | '
where-object-filterscript {$_. Lastwritetime-gt "2011-01-01")-and '
($_. Psiscontainer-ne $True)} | Select-object FullName

If you want to delete, Select-object FullName changed to Remove-item-force

The batch processing of the specified date is still convenient, and if you want to specify an old file to delete n days ago, it's a bit of a hassle.
The following example uses BAT to delete a file that has been modified by the specified date. Note is modified, not created, only
Have DIR/TC to see file creation time, default dir is DIR/TW

Sender: Nwn (Lie), letter area: DOS
Title: Re: (for command) bulk delete files created within a time period?
Letter Station: Water Wood Community (Sat June 7 08:39:39 2008), station

@echo off
cd/d your directory for
%%f in (*) do if "%%~TF" GTR "2008-04-01" del "%%f"

If you want to include subdirectories, use FOR/R. %%f in .....

"In the Masterpiece of Justzhou (Yushu), said:"
: For example, to delete all files created after 2008-04-01 in a directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.