Reputation: 611
I am trying to find the time difference between the last updated time and current time for a file. How do I extract TotalMinutes data from the output?
$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
Upvotes: 14
Views: 53587
Reputation: 2754
Assuming the file exists, here's a one-liner:
((Get-Date) - (Get-ChildItem 'C:\Users\ABCD\Documents\command.txt').LastWriteTime).TotalMinutes
You can either let it echo to the screen as is, or you can assign it to a variable if you want to retain it.
Upvotes: 1
Reputation: 71
$StartDate=(GET-DATE)
#wait a few seconds
$EndDate=(GET-DATE)
$diff = NEW-TIMESPAN -Start $StartDate -End $EndDate
Write-Output "Time difference is: $diff"
$diff
#to see minutes only
$diff.Minutes
#or seconds
$diff.Seconds
Upvotes: 6
Reputation: 31
You can use the below command to get just TotalMinutes
$mins = $DURATION.TotalMinutes
Upvotes: 3
Reputation: 1716
You will not need a loop, after getting a single file:
$Files = gci "C:\Users\ABCD\Documents\command.txt"
ForEach ($File in $Files){
$FileDate = $File.LastWriteTime
}
In this case, $Files
might as well be $File
, making the loop completely redundant:
$File = gci "C:\Users\ABCD\Documents\command.txt"
$FileDate = $File.LastWriteTime
In the exact same way you extracted LastWriteTime
, you can get TotalMinutes
:
$Date = Get-Date
$DURATION = $Date - $FileDate
$DURATION.TotalMinutes
Upvotes: 13
Reputation: 2413
Here is a complete answer:
$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)"
Upvotes: 2