How to retrieve file modification time through WMI and PowerShell

I needed to get a file modification timestamp through WMI (for an irrelevant reason, I couldn’t use the UNC path and PowerShell’s get-childitem cmdlet). So, WMI it had to be…

$file = gwmi -computername remoteComputer -query "select * from cim_datafile where name = 'C:\file.txt' "  
$age = (get-date) - $file.convertToDateTime($file.LastModified)  
return $age.seconds

Pretty quick and painless!

Note: you could get (m)any other file attributes by slightly altering these lines. Remember: the get-member cmdlet is your friend!

gwmi -computername remoteComputer -query "select * from cim_datafile where name = 'C:\file.txt' " | gm

That line will return a list of file attributes at your disposal.