Powershell代码,用于删除目录中除最近2天数据和2个文件夹之外的所有文件和文件夹

时间:2016-03-18 03:04:41

标签: powershell

# set folder path
$dump_path = "C:\shares\*"
$Eliminate = @("*dump*", "*Perform")

# set min age of files
#$max_days = "-2" | Where-Object { $_.LastWriteTime -lt $del_date } 

# get the current date
#$curr_date = Get-Date

# determine how far back we go based on current date
#$del_date = $curr_date.AddDays($max_days)

# delete the files
Remove-Item -Recurse C:\shares\* -exclude $Eliminate

1 个答案:

答案 0 :(得分:0)

你可以这样做:

Get-Childitem C:\shares -recurse | where {($_.creationtime -gt $del_date) -and ($_.name -notlike "*dump*" -and $_.name -notlike "*perform*")} | remove-item

此刻我有点生疏,所以有人可能会提出更优雅的解决方案