首页 文章

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

提问于
浏览
152

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

10 回答

  • 15

    给定的答案只会删除文件(这无疑是本文 Headers 中的内容),但这里有一些代码会首先删除超过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代码显示为handy reusable functions on my blog .

  • 260

    只是简单(PowerShell V5)

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

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

    $root  = 'C:\root\folder'
    $limit = (Get-Date).AddDays(-15)
    
    Get-ChildItem $root -Recurse | ? {
      -not $_.PSIsContainer -and $_.CreationTime -lt $limit
    } | Remove-Item
    
  • 30

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

    试试这个:

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

    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
    
  • 1

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

    ls -file | where { (get-date) - $_.creationtime -gt 15. } | Remove-Item -Verbose
    
  • -1
    $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
    

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

  • 2

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

    dir C:\locationOfFiles -ErrorAction SilentlyContinue | Where { ((Get-Date)-$_.LastWriteTime).days -gt 15 } | Remove-Item -Force
    
  • 0
    #----- 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"
        }
    }
    

相关问题