Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PowerShell LastWriteTime compare

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 convert "@{LastWriteTime=...date...}" to "System.DateTime".

like image 661
Steven Rogers Avatar asked Jun 19 '09 16:06

Steven Rogers


2 Answers

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.

like image 170
JaredPar Avatar answered Sep 24 '22 03:09

JaredPar


(ls $source).LastWriteTime 

("ls", "dir", or "gci" are the default aliases for Get-ChildItem.)

like image 27
brianary Avatar answered Sep 23 '22 03:09

brianary