New OlderThan and NewerThan parameters for Get-ChildItem cmdlet
See original GitHub issueSummary of the new feature/enhancement
Today, to find elements of the file system older than (or newer than) the specified date, we are forced to do something like
$Path = "C:\temp"
$Daysback = "-30"
$CurrentDate = Get-Date
$DateOlderThan = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse ( | Where-Object { $_.LastWriteTime -lt $DateOlderThan }
At the same time, Test-Path cmdlet supports the parameters -OlderThan <datetime>
and -NewerThan <datetime>
Proposal
Add -OlderThan <datetime>
and -NewerThan <datetime>
parameters to Get-ChildItem
cmdlet of the FileSystem provider.
As result we get more short form and more fast and efficient filtering without extra file object retrieving and piping.
$Path = "C:\temp"
$Daysback = "-30"
$CurrentDate = Get-Date
$DateOlderThan = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -OlderThan $DateOlderThan
Proposed technical implementation details (optional)
Example of the parameters definition https://github.com/PowerShell/PowerShell/blob/281b437a65360ae869d40f3766a1f2bbba786e5e/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L7805
Place for new parameters https://github.com/PowerShell/PowerShell/blob/281b437a65360ae869d40f3766a1f2bbba786e5e/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L7531
Place for injection new filtering code (for Recurse parameter) https://github.com/PowerShell/PowerShell/blob/281b437a65360ae869d40f3766a1f2bbba786e5e/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L1779
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:9
Top GitHub Comments
A switch parameter sounds fine.
Regarding the parameter names
-OlderThan
and-NewerThan
, they are somewhat ambiguous… It’s not totally obvious whether a comparison is performed againstLastWriteTime
orCreationTime
.The documentation of these parameters in relation to
Test-Path
is currently very poor:So whatever the final implementation of these new parameters is, it would be great if there’s some meaningful documentation to accompany them.
You might also consider to accept (besides a
DateTime
type) aTimeSpan
type: