Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting by date PowerShell

Tags:

powershell

I'm trying to find all files that include some string and are not older than "x" days. Those files have to be sorted and then sent in .txt format.

The code seemed fine to me, but it doesn´t filter files by date. All other cmdlets work as intended, but the part Where-Object {$_.LastWriteTime -ge (Get-Date).AddDays(-$days)} doesn´t seem to be working at all.

Do you have any recommendations how to fix it?

Get-ChildItem -Path $path -Recurse |
    Where-Object {$_.LastWriteTime -ge (Get-Date).AddDays(-$days)} |
    Select-String -Pattern $searched |
    Group-Object -Property Path, Filename |
    select Name |
    Out-File -Filepath C:\tmp\output.txt
like image 901
NotThatDumbGuy123 Avatar asked Sep 06 '25 03:09

NotThatDumbGuy123


1 Answers

| Sort-Object -Property propertyName -Descending

cmdlet to sort by property value reference link - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object?view=powershell-6

like image 70
Vinod kumar G Avatar answered Sep 09 '25 18:09

Vinod kumar G