应用场景
技术细节
单文件时间属性修改
#最后访问时间
$(Get-Item "C:/path/to/yourfile").LastAccessTime=$(Get-Date "1999/1/1 12:12:12")
#最后保存时间
$(Get-Item "C:/path/to/yourfile").LastWriteTime=$(Get-Date "1999/1/1 12:12:12")
#文件创建时间
$(Get-Item "C:/path/to/yourfile").CreationTime=$(Get-Date "1999/1/1 12:12:12")
多文件时间属性批量修改
#当前目录所有文件及文件夹时间属性修改
foreach($n in $(Get-ChildItem).Name)
{
$(Get-Item $n).LastAccessTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).LastWriteTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).CreationTime=$(Get-Date "1999/1/1 12:12:12")
}
#仅修改当前目录文件的时间属性(不包含文件夹)
foreach($n in $(Get-ChildItem -File).Name)
{
$(Get-Item $n).LastAccessTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).LastWriteTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).CreationTime=$(Get-Date "1999/1/1 12:12:12")
}
#仅修改当前目录文件夹的时间属性(不包含文件)
foreach($n in $(Get-ChildItem -Directory).Name)
{
$(Get-Item $n).LastAccessTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).LastWriteTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).CreationTime=$(Get-Date "1999/1/1 12:12:12")
}
#修改当前目录下所有文件及文件夹的时间属性(包括子文件夹)
foreach($n in $(Get-ChildItem -Recurse).FullName)
{
$(Get-Item $n).LastAccessTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).LastWriteTime=$(Get-Date "1999/1/1 12:12:12")
$(Get-Item $n).CreationTime=$(Get-Date "1999/1/1 12:12:12")
}
#将当前目录下所有文件及文件夹时间属性设置为随机时间
#示例时间随机范围为2019/1/2至操作系统当前时间
foreach($n in $(Get-ChildItem -Recurse).FullName)
{
$(Get-Item $n).LastAccessTime=$(Get-Random -Minimum $(Get-Date "2019/1/2").Ticks -Maximum $(Get-Date).Ticks |Get-Date)
$(Get-Item $n).LastWriteTime=$(Get-Random -Minimum $(Get-Date "2019/1/2").Ticks -Maximum $(Get-Date).Ticks |Get-Date)
$(Get-Item $n).CreationTime=$(Get-Random -Minimum $(Get-Date "2019/1/2").Ticks -Maximum $(Get-Date).Ticks |Get-Date)
}