It’s Wednesday, so you know what that means? A new one liner that I’m hoping is useful for you as it has been useful for me. I’ve talked about date and time before, but that was for selecting dates. What about if you wanted to figure out how many days till the end of the year, or how many seconds from current time to 3 months and 5 days from now is? Or when the last time a file was modified?
New-TimeSpan is your answer. Want to know how many days and seconds it is till the end of the year?
New-Timespan -end 12/31/2022
You can use the -Start parameter but isn’t needed unless you want to use a different start date.
What about if you only need the days because you will be putting it into a variable to use later on?
New-TimeSpan -end 12/31/2022 | select days
The final one I’ll leave you with is:
Get-ChildItem $PSHOME\onelinerwednesday.ps1 | New-TimeSpan
This will tell you the last time the file was updated. This can be great if you have scheduled tasks that run or if you need to find files that were edited in the last 3 hours or 3 years. This can also be used on any command that uses the -LastWriteTime property.
Let me know if you have used this before or what you intend to try it out on.
Leave a Reply