使用PowerShell脚本以分钟为单位查找时差

时间:2016-05-30 09:09:57

标签: windows powershell

我正在尝试查找文件的上次更新时间和当前时间之间的时差。如何从输出中提取TotalMinutes数据?

$Date = Get-Date
$Files = gci "C:\Users\ABCD\Documents\command.txt"
ForEach ($File in $Files){
    $FileDate = $File.LastWriteTime
}
$DURATION=$Date-$FileDate
Echo $DURATION

Output is coming as below
Days              : 0
Hours             : 2
Minutes           : 21
Seconds           : 37
Milliseconds      : 311
Ticks             : 84973115857
TotalDays         : 0.0983485137233796
TotalHours        : 2.36036432936111
TotalMinutes      : 141.621859761667
TotalSeconds      : 8497.3115857
TotalMilliseconds : 8497311.5857

3 个答案:

答案 0 :(得分:2)

获取单个文件后,您不需要循环:

$Files = gci "C:\Users\ABCD\Documents\command.txt"
ForEach ($File in $Files){
    $FileDate = $File.LastWriteTime
}

在这种情况下,$Files也可能是$File,使循环完全冗余:

$File = gci "C:\Users\ABCD\Documents\command.txt"
$FileDate = $File.LastWriteTime

与提取LastWriteTime的方式完全相同,您可以获得TotalMinutes

$Date = Get-Date
$DURATION = $Date - $FileDate
$DURATION.TotalMinutes

答案 1 :(得分:0)

这是一个完整的答案:

$Date = Get-Date
$Files = gci "C:\Users\ABCD\Documents\command.txt"
ForEach ($File in $Files){
    $FileDate = $File.LastWriteTime
}
$DURATION=$Date-$FileDate
Write-Host "$($DURATION.TotalMinutes)"

答案 2 :(得分:0)

您可以使用以下命令获取TotalMinutes

$mins = $DURATION.TotalMinutes