Reputation: 155
$tmpCreated = dir | Sort CreationTime -Descending | Select CreationTime -First 1
$tmpCreated = [string]$tmpCreated
$tmpCreated = $tmpCreated.split("=")[1]
$tmpCreated = $tmpCreated.split("}")[0]
Here is the variable I want to get as a DateTime. I want to compare two dates with this command :
$timeDiff = new-timespan –Start $tmpLast –End $tmpCreated
$tmpLast
works fine. But I get an error when i try to launch, saying the -End parameter cannot be used because the $tmpCreated
conversion failed. But here is the string it contains : 11/30/2015 11:57:01
. So, does someone know what's wrong with this ?
Upvotes: 1
Views: 191
Reputation: 200233
The CreationTime
property already contains a DateTime
object, so you simply need to expand the property:
$tmpCreated = Get-ChildItem |
Sort-Object CreationTime -Descending |
Select-Object -Expand CreationTime -First 1
$timeDiff = New-TimeSpan –Start $tmpLast –End $tmpCreated
Upvotes: 5
Reputation: 17797
You don't need to convert the date to a string to use it with New-TimeSpan
$tmpCreated = dir | Sort CreationTime -Descending | Select CreationTime -First 1
$timeDiff = new-timespan –Start $tmpLast –End $tmpCreated.CreationTime
will do fine.
Upvotes: 2