#定义执行的目录 $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
时间: 2024-10-13 11:59:03