How to group files by date using PowerShell?

I have a folder with 2000+ files. I want to count files by date.

so that:

  Mode                LastWriteTime     Length Name  
   ----                -------------     ------ ----  
   -a---        2010-03-15  12:54 AM   10364953 file1.txt  
   -a---        2010-03-15   1:07 AM   10650503 file2.txt  
   -a---        2010-03-16   1:20 AM   10118657 file3.txt  
   -a---        2010-03-16   1:33 AM    9735542 file4.txt  
   -a---        2010-03-18   1:46 AM   10666979 file5.txt  

I would like to see:

Date         Count
----------   ------
2010-03-15   2  
2010-03-16   2  
2010-03-18   1

Thank!

+3
source share
2 answers

A group object can easily cope with this task:

Get-ChildItem | Group {$_.LastWriteTime.ToString("yyyy-MM-dd")} | Sort Name

If you want to see only the date and cap in the format table, as shown below:

Get-ChildItem | Group {$_.LastWriteTime.ToString("yyyy-MM-dd")} | Sort Name | 
    Format-Table Name,Count -auto
+12
source

You can use the hash table to collect the information you need. gci is an alias from get-childitem.

 $dict = new-object -t system.collections.hashtable
 gci * |? { $dict[$_.lastwritetime.date]++ }
 $dict
+2
source

Source: https://habr.com/ru/post/1738597/


All Articles