繁体   English   中英

使用 PowerShell 删除超过 15 天的文件

[英]Delete files older than 15 days using PowerShell

我只想删除特定文件夹中超过 15 天前创建的文件。 我怎么能用 PowerShell 做到这一点?

给出的答案只会删除文件(诚然,这就是本文标题中的内容),但这里有一些代码将首先删除所有 15 天以上的文件,然后递归删除任何可能已经留下的空目录后面。 我的代码还使用-Force选项来删除隐藏文件和只读文件。 另外,我选择不使用别名,因为 OP 是 PowerShell 的新手,可能不明白gci , ? , %等是。

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

当然,如果您想在实际删除之前查看将删除哪些文件/文件夹,您只需将-WhatIf开关添加到两行末尾的Remove-Item cmdlet 调用即可。

如果您只想删除 15 天内未更新的文件,而不是 15 天前创建的文件,那么您可以使用$_.LastWriteTime而不是$_.CreationTime

此处显示的代码与 PowerShell v2.0 兼容,但我也在我的博客上将此代码和速度更快的 PowerShell v3.0 代码显示为方便的可重用函数

只是简单(PowerShell V5)

Get-ChildItem "C:\temp" -Recurse -File | Where CreationTime -lt  (Get-Date).AddDays(-15)  | Remove-Item -Force

另一种方法是从当前日期减去 15 天并将CreationTime与该值进行比较:

$root  = 'C:\root\folder'
$limit = (Get-Date).AddDays(-15)

Get-ChildItem $root -Recurse | ? {
  -not $_.PSIsContainer -and $_.CreationTime -lt $limit
} | Remove-Item

基本上,您遍历给定路径下的文件,从当前时间中减去找到的每个文件的CreationTime ,然后与结果的Days属性进行比较。 -WhatIf开关将告诉您在不实际删除文件的情况下会发生什么(哪些文件将被删除),删除开关以实际删除文件:

$old = 15
$now = Get-Date

Get-ChildItem $path -Recurse |
Where-Object {-not $_.PSIsContainer -and $now.Subtract($_.CreationTime).Days -gt $old } |
Remove-Item -WhatIf

试试这个:

dir C:\PURGE -recurse | 
where { ((get-date)-$_.creationTime).days -gt 15 } | 
remove-item -force

Esperento57 的脚本在较旧的 PowerShell 版本中不起作用。 这个例子做:

Get-ChildItem -Path "C:\temp" -Recurse -force -ErrorAction SilentlyContinue | where {($_.LastwriteTime -lt  (Get-Date).AddDays(-15) ) -and (! $_.PSIsContainer)} | select name| Remove-Item -Verbose -Force -Recurse -ErrorAction SilentlyContinue

另一种选择(15. 自动输入 [timespan]):

ls -file | where { (get-date) - $_.creationtime -gt 15. } | Remove-Item -Verbose

如果您在 Windows 10 机器上遇到上述示例的问题,请尝试将.CreationTime替换为.LastwriteTime 这对我有用。

dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force
$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -Recurse

这将删除旧文件夹及其内容。

#----- Define parameters -----#
#----- Get current date ----#
$Now = Get-Date
$Days = "15" #----- define amount of days ----#
$Targetfolder = "C:\Logs" #----- define folder where files are located ----#
$Extension = "*.log" #----- define extension ----#
$Lastwrite = $Now.AddDays(-$Days)

#----- Get files based on lastwrite filter and specified folder ---#
$Files = Get-Children $Targetfolder -include $Extension -Recurse | where {$_.LastwriteTime -le "$Lastwrite"}

foreach ($File in $Files)
{
    if ($File -ne $Null)
    {
        write-host "Deleting File $File" backgroundcolor "DarkRed"
        Remove-item $File.Fullname | out-null
    }
    else
        write-host "No more files to delete" -forgroundcolor "Green"
    }
}

以下代码将删除文件夹中超过 15 天的文件。

$Path = 'C:\Temp'
$Daysback = "-15"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse  | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM