Simple PowerShell LastWriteTime compare

前端 未结 7 878
我寻月下人不归
我寻月下人不归 2021-02-12 08:42

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 differe

7条回答
  •  生来不讨喜
    2021-02-12 08:53

    I can't fault any of the answers here for the OP accepted one of them as resolving their problem. However, I found them flawed in one respect. When you output the result of the assignment to the variable, it contains numerous blank lines, not just the sought after answer. Example:

    PS C:\brh> [datetime](Get-ItemProperty -Path .\deploy.ps1 -Name LastWriteTime).LastWriteTime
    
    Friday, December 12, 2014 2:33:09 PM
    
    
    
    PS C:\brh> 
    

    I'm a fan of two things in code, succinctness and correctness. brianary has the right of it for succinctness with a tip of the hat to Roger Lipscombe but both miss correctness due to the extra lines in the result. Here's what I think the OP was looking for since it's what got me over the finish line.

    PS C:\brh> (ls .\deploy.ps1).LastWriteTime.DateTime
    Friday, December 12, 2014 2:33:09 PM
    
    PS C:\brh> 
    

    Note the lack of extra lines, only the one that PowerShell uses to separate prompts. Now this can be assigned to a variable for comparison or, as in my case, stored in a file for reading and comparison in a later session.

提交回复
热议问题