LoginSignup
4
9

More than 5 years have passed since last update.

PowerShellで期間指定してファイルを削除

Last updated at Posted at 2017-12-03
$path = “path\to\logs”
ls $path | ? {$_.LastWriteTime -lt <日時>} | rm
  • ls $path ... Get-ChildItem
  • ? {$_.LastWriteTime -lt <日時>} ... 更新日時が~より前
  • rm ... Remove-Item パイプで渡されたファイルを削除する

日時の指定方法

指定方法 内容
(Get-Date).AddHours(-n) n時間前
(Get-Date).AddDays(-n) n日前
(Get-Date).AddMonths(-n) nヶ月前
(Get-Date).AddYears(-n) n年前

更新日時が一ヶ月以前のファイルを削除する例

$path = “D:\logs”
ls $path | ? {$_.LastWriteTime -lt (Get-Date).AddMonths(-1)} | rm
4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9