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

使用PowerShell删除超过15天的文件,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我只想删除15天前在特定文件夹中创建的文件。我如何使用PowerShell实现这一点 试试这个: dir C:\PURGE -recurse | where { ((get-date)-$_.creationTime).days -gt 15 } | remove-item -force 基本上,您迭代给定路径下的文件,从当前时间中减去找到的每个文件的CreationTime,并与结果的Days属性进行比较。-WhatIf开关将告诉您在不实际删除文件(哪些文件将被删除)的情况下会发生什么,

我只想删除15天前在特定文件夹中创建的文件。我如何使用PowerShell实现这一点

试试这个:

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

基本上,您迭代给定路径下的文件,从当前时间中减去找到的每个文件的
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

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

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

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

给出的答案只会删除文件(诚然,这是本文标题中的内容),但下面的一些代码将首先删除所有超过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调用

这里显示的代码与PowerShell v2.0兼容,但我也将此代码和更快的PowerShell v3.0代码显示为

这将删除旧文件夹和it内容。

只需简单操作(PowerShell V5)

#----- 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"
    }
}

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]:


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

dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force

我认为最后一个递归太多了,不是吗?目录列表是递归的,删除项目时不应包含child,对吗?如果您使用的目录有两个目录深,则需要第二次递归。感谢您没有使用alias。对于一个刚到PoSeBand的人,通过谷歌搜索找到这篇文章,我认为你的答案是最好的。我尝试了“致命的狗的建议”,不管我指定的是15还是35,不同的文件创建日期要追溯到几个月(或最近)。它正在删除目录的全部内容。如果文件可能正在使用,还需要在RemoveItem命令中添加“-ErrorAction SilentlyContinue”。谢谢!我使用$\.LastwriteTime而不是$\.CreationTime该脚本中的第二个命令总是会出现一个错误,即Get ChildItem无法找到路径的一部分。它获取一个未找到目录异常。然而,它删除空文件夹没有问题。不确定它在工作时为什么会出错。此外,它永远不会到达else语句,因为如果
$Files
为空,它将不会进入foreach语句。您应该将foreach放在if语句中。@mati实际上它可以到达else语句。我们有一个类似的基于文件列表的for循环,它定期使用$file变量作为nullI进入for循环,我猜是这样的-只是在同一个脚本中插入;大多数答案都使用CreationTime,但在复制文件时会重置它,因此您可能无法获得所需的结果。LastWriteTime与Windows资源管理器中的“修改日期”相对应。我现在已更新了答案以排除目录,谢谢。
LastWriteTime
CreationTime
不同,
LastWriteTime
在每次修改文件时都会更新。为此,特别是包括用于测试的“WhatIf”开关。
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
ls -file | where { (get-date) - $_.creationtime -gt 15. } | Remove-Item -Verbose
dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force