Powershell - 设置保留数据删除
Powershell - set up data removal with retention
我需要一个 PS 脚本来删除目录的所有子文件夹,除了那些:
- 过去 7 天的所有内容
- 上个月每周的一个最新文件夹
- 过去一年每个月的一个最新文件夹
我是 PS 的新手,并且已经花了很多时间在挣扎中。讨厌哭求救命,但我想,这是正确的时刻。
谢谢!
现在完成(:
# Initiate the intervals in days by which the folders will be kept
$savedays = @('7', '14', '21', '30', '60', '90', '120', '150', '180', '210', '240', '270', '300', '330', '360')
# initiate array of folders to be deleted
$killlist = @()
# Start a for loop until hitting the one before last element of $savedays
For ($i=0;$i -lt ($savedays.Length -1 ); $i++) {
# Get list of folders in the $path_main
$killlist += @(Get-ChildItem $path_main |
# Newest first
Sort-Object -Property LastWriteTime -Descending |
# Between one and next elevement of #savedays, i.e. between 7 days ago and 14 days ago
Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-$savedays[$i]) -and $_.LastWriteTime -gt (get-date).AddDays(-$savedays[$i+1])} |
# Exclude the most recent folder from the aforementioned period
Select-Object -Skip 1
)
}
# delete folders
foreach ($folder in $killlist) {
# delete the folder including contents
Remove-Item $folder.fullname –recurse
}
我需要一个 PS 脚本来删除目录的所有子文件夹,除了那些:
- 过去 7 天的所有内容
- 上个月每周的一个最新文件夹
- 过去一年每个月的一个最新文件夹
我是 PS 的新手,并且已经花了很多时间在挣扎中。讨厌哭求救命,但我想,这是正确的时刻。
谢谢!
现在完成(:
# Initiate the intervals in days by which the folders will be kept
$savedays = @('7', '14', '21', '30', '60', '90', '120', '150', '180', '210', '240', '270', '300', '330', '360')
# initiate array of folders to be deleted
$killlist = @()
# Start a for loop until hitting the one before last element of $savedays
For ($i=0;$i -lt ($savedays.Length -1 ); $i++) {
# Get list of folders in the $path_main
$killlist += @(Get-ChildItem $path_main |
# Newest first
Sort-Object -Property LastWriteTime -Descending |
# Between one and next elevement of #savedays, i.e. between 7 days ago and 14 days ago
Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-$savedays[$i]) -and $_.LastWriteTime -gt (get-date).AddDays(-$savedays[$i+1])} |
# Exclude the most recent folder from the aforementioned period
Select-Object -Skip 1
)
}
# delete folders
foreach ($folder in $killlist) {
# delete the folder including contents
Remove-Item $folder.fullname –recurse
}