views:

1253

answers:

2

I need a Powershell script that can access a file's properties and discover the LastWriteTime property and compare it with the current date and return the date difference.

I have something like this...

$writedate = Get-ItemProperty -Path $source -Name lastwritetime

...but I can not cast the lastwritetime to a "DateTime" datatype. It says, "Cannot concert "@{lastwritetime=...date...}" to "System.DateTime".

Any help would be highly appreciated!

Thanks to everyone in advance.

+3  A: 

Try the following.

$d = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime

This is part of the item property weirdness. When you run Get-ItemProperty it does not return the value but instead the property. You have to use one more level of indirection to get to the value.

JaredPar
That works! Thanks!
Steven Rogers
+1  A: 

ls | % {(get-date) - $_.LastWriteTime }
Can works, to retrieve the diff, you can replace ls with a single file.

Raoul Supercopter
The diff works! thanks!
Steven Rogers