通过PowerShell更改上次修改的日期或时间


18

是否可以通过PowerShell更改文件或文件夹的上次修改日期/时间?

我有一个文件夹,folder1/并且我想通过PowerShell更改该文件夹的上次修改日期和时间。

Answers:


23

获取文件对象,然后设置属性:

$file = Get-Item C:\Path\TO\File.txt
$file.LastWriteTime = (Get-Date)

或文件夹:

$folder = Get-Item C:\folder1
$folder.LastWriteTime = (Get-Date)

6

下面的方法来解释这里为我工作。所以我用了:

Get-ChildItem  C:\testFile1.txt | % {$_.LastWriteTime = '01/11/2005 06:01:36'}

不要被“ get- *”命令弄糊涂了……不管它是get而不是write或其他东西,它都会起作用。还要注意,如在源代码中所写,您需要使用您配置的数据格式,而在我上面的示例中可能不需要。


4

是的,可以更改最后修改日期。这是一个班轮的例子

powershell foreach($file in Get-ChildItem folder1) {$(Get-Item $file.Fullname).lastwritetime=$(Get-Date).AddHours(-5)}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.