Finding Newest or Oldest Files

by Dec 5, 2012

In PowerShell 3.0, Measure-Object can be applied not just to numeric data but to anything that is comparable. In PowerShell 2.0, this line would return the smallest and largest file size in your Windows folder:

Get-ChildItem $env:windir | Measure-Object -Property Length -Minimum -Maximum | Select-Object -Property Minimum,Maximum 

In PowerShell 3.0, you could also measure properties like LastWriteTime, telling you the oldest and newest dates:

Get-ChildItem $env:windir | Measure-Object -Property LastWriteTime -Minimum -Maximum | Select-Object -Property Minimum,Maximum 

Or, you could get the minimum and maximum start times of all the running processes. Make sure you use Where-Object to exclude any process that has no StartTime value:

Get-Process | Where-Object StartTime | Measure-Object -Property StartTime -Minimum -Maximum | Select-Object -Property Minimum,Maximum 

Twitter This Tip! ReTweet this Tip!