ファイルの更新日時を変更する、様々なPowerShellのコマンドのサンプルを用意してみました。
ファイルの更新日時変更サンプル集
特定のファイルの更新日時を変更する
特定のファイルの更新日時を変更する場合は下記の通りになります。
ちなみに他のコマンドサンプルもこちらのコマンドをベースにしたものになります。
コマンド
Set-ItemProperty test1_abc_0002.txt -name LastWriteTime -value '2022/11/13 13:00:00'
テスト実行
実行前

コマンド実行

実行後

フォルダ内のファイルの更新日時をまとめて変更する
フォルダ内のファイルの更新日時を、まとめて変更するコマンドになります。
ただしこの方法だとサブフォルダの更新日時は、更新されません。サブフォルダを含む方法は次の章で紹介します。
コマンド
Set-ItemProperty * -name LastWriteTime -value '2022/10/22 00:01:02'
テスト実行
実行前

コマンド実行

実行後

サブフォルダを含めてフォルダ内のファイルの更新日時をまとめて変更する
サブフォルダも含めてフォルダ内のファイルの更新日時を一括で変更します。
Get-ChildItemで一度ファイルリストを取得して、Set-ItemPropertyに渡す必要があります。
コマンド
get-childitem * -Recurse | %{Set-ItemProperty $_.fullname -Name LastWriteTime -Value "2022/10/01 18:00:00"}
テスト実行
実行前
filesフォルダと、そのサブフォルダのsubフォルダにファイルがあります。

コマンド実行

実行後


特定の拡張子のファイルの更新日時を変更する
*の部分を変更すると、特定のファイルの更新日時を変更できます。
コマンド
Set-ItemProperty *.log -name LastWriteTime -value '2022/10/11 01:01:02'
テスト実行
実行前

コマンド実行

実行後

フォルダ内(サブフォルダ含む)のファイルの更新日時を現在日時にする
コマンド
get-childitem * -Recurse | %{Set-ItemProperty $_.fullname -Name LastWriteTime -Value $(Get-Date)}
テスト実行
実行前
現在日時は2022/11/15 21:48です。


コマンド実行

実行後

参考情報
環境
下記の環境で作成・実行しております。
No. | 環境 | バージョン |
---|---|---|
1 | OS | Windows10 |
2 | PowerShell | 5.1 |
以上です。