1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#定义执行的目录
$path  "D:\logfile\"
 
#获取30之前修改的文件列表:
Get-ChildItem  -Path  $path  -Recurse -ErrorAction:SilentlyContinue | `
Where-Object  -FilterScript {((( get-date ) - ( $_ .LastWriteTime)).days  -gt  30 `
-and  $_ .PsISContainer  -ne  $False )} |  Select-Object  FullName  #PsISContainer=True is file,False is folder
 
#删除30天之前修改的文件:
Get-ChildItem  -Path  $path  -Recurse -ErrorAction:SilentlyContinue | `
Where-Object  -FilterScript {((( get-date ) - ( $_ .LastWriteTime)).days  -gt  30 `
-and  $_ .PsISContainer  -ne  $False )} |  Remove-Item  -Recurse
 
#删除30天之前修改的文件(包括删除只读、隐藏):
#Get-ChildItem -Path D:\test -Force -Recurse -ErrorAction:SilentlyContinue | `
#Where-Object -FilterScript {(((get-date) - ($_.CreationTime)).days -gt 30 `
#-and $_.PsISContainer -ne $True)} | Remove-Item -Force















本文转自bard_zhang51CTO博客,原文链接:http://blog.51cto.com/timefiles/1855352 ,如需转载请自行联系原作者