question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New OlderThan and NewerThan parameters for Get-ChildItem cmdlet

See original GitHub issue

Summary 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

1reaction
thecliguycommented, Jan 6, 2021

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 against LastWriteTime or CreationTime.

The documentation of these parameters in relation to Test-Path is currently very poor:

-NewerThan

Specify a time as a DateTime object.
-OlderThan

Specify a time as a DateTime object.

So whatever the final implementation of these new parameters is, it would be great if there’s some meaningful documentation to accompany them.

1reaction
iRon7commented, Dec 24, 2020

You might also consider to accept (besides a DateTime type) a TimeSpan type:

$Path = 'C:\temp'
$Daysback = New-TimeSpan -Days -30

Get-ChildItem $Path -Recurse -OlderThan $Daysback
Read more comments on GitHub >

github_iconTop Results From Across the Web

Test-Path - PowerShell
The Test-Path cmdlet determines whether all elements of the path exist. ... The NewerThan parameter works only in file system drives. PowerShell
Read more >
Get-ChildItem - PowerShell
The Get-ChildItem cmdlet uses the Path parameter to specify the directory C:\Test . Get-ChildItem displays the files and directories in the PowerShell console....
Read more >
New Powershell 3.0 params for Get-Child Item and Test-Path
Olderthan. The first two parameters are new to Get-Childitem cmdlet and are used to return either the files or the folder in a...
Read more >
How to Use the PowerShell Test-Path Cmdlet
The NewerThan parameter accepts a string or a DateTime object to represent a timestamp to check against. For example, to check whether or...
Read more >
How to use Test-Path cmdlet in PowerShell
The PowerShell Test-Path cmdlet comes with different parameters that we can ... The parameters -NewerThan and -OlderThan requires a date or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found