Simple PowerShell LastWriteTime compare Simple PowerShell LastWriteTime compare powershell powershell

Simple PowerShell LastWriteTime compare


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.


(ls $source).LastWriteTime

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


I have an example I would like to share

$File = "C:\Foo.txt"#retrieves the Systems current Date and Time in a DateTime Format$today = Get-Date#subtracts 12 hours from the date to ensure the file has been written to recently$today = $today.AddHours(-12)#gets the last time the $file was written in a DateTime Format$lastWriteTime = (Get-Item $File).LastWriteTime#If $File doesn't exist we will loop indefinetely until it does exist.# also loops until the $File that exists was written to in the last twelve hourswhile((!(Test-Path $File)) -or ($lastWriteTime -lt $today)){    #if a file exists then the write time is wrong so update it    if (Test-Path $File)    {        $lastWriteTime = (Get-Item $File).LastWriteTime    }    #Sleep for 5 minutes    $time = Get-Date    Write-Host "Sleep" $time    Start-Sleep -s 300;}