PowerShell 技能连载 - 删除日期最早的日志文件

如果您正在将日志活动写入文件,可能需要清除一些东西,例如在增加一个新文件的时候总是需要删除最旧的日志文件。

以下是一个简单的实现:

1
2
3
4
5
6
7
8
9
10
11
12
# this is the folder keeping the log files
$LogFileDir = "c:\myLogFiles"

# find all log files...
Get-ChildItem -Path $LogFileDir -Filter *.log |
# sort by last change ascending
# (oldest first)...
Sort-Object -Property LastWriteTime |
# take the first (oldest) one
Select-Object -First 1 |
# remove it (remove -whatif to actually delete)
Remove-Item -WhatIf

如果只希望保留最新的 5 个文件,请像这样更改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# this is the folder keeping the log files
$LogFileDir = "c:\myLogFiles"
$Keep = 5

# find all log files...
$files = @(Get-ChildItem -Path $LogFileDir -Filter *.log)
$NumberToDelete = $files.Count - $Keep

if ($NumberToDelete -gt 0)
{
$files |
# sort by last change ascending
# (oldest first)...
Sort-Object -Property LastWriteTime |
# take the first (oldest) one
Select-Object -First $NumberToDelete |
# remove it (remove -whatif to actually delete)
Remove-Item -WhatIf
}

PowerShell 技能连载 - 删除日期最早的日志文件

http://blog.vichamp.com/2019/01/07/deleting-the-oldest-log-file/

作者

吴波

发布于

2019-01-07

更新于

2022-07-06

许可协议

评论