如何使用Powershell对唯一的最新文件进行排序

时间:2016-01-21 13:37:42

标签: powershell

在我的文件夹testtemp中,我有这个文件

 PS C:\scripts\testtemp> Get-ChildItem | select name,lastwritetime

 Name                                                        LastWriteTime
 ----                                                        -------------
 AABASSI.TMC049913.mapdrives.txt                             27/02/2015 11:01:11
 AABASSI.TMC049916.mapdrives.txt                             02/03/2015 14:29:31
 AABASSI.TMC050020.mapdrives.txt                             26/11/2014 18:08:48
 aabassi.TMS064845.mapdrives.txt                             06/03/2015 14:14:50
 aaboud.DLECLERCQPC.mapdrives.txt                            03/04/2015 09:02:03
 aaboud.FHP030221.mapdrives.txt                              30/12/2014 15:05:04
 aaboud.FHP045846.mapdrives.txt                              18/11/2014 10:15:05
 Aaccus.FHP047416.mapdrives.txt                              25/07/2014 08:16:19
 AADRIENVASSE.PHP049659.mapdrives.txt                        02/10/2015 14:18:15
 AADRIENVASSE.RBXXAMET10.mapdrives.txt                       06/08/2015 10:33:33
 aagostini.FHP047082.mapdrives.txt                           05/03/2015 13:38:07
 aagricole.PHP053341.mapdrives.txt                           30/03/2015 15:09:03
 aagricole.TMS064745.mapdrives.txt                           25/03/2015 12:32:06
 aaiad.PHP053346.mapdrives.txt                               09/06/2015 09:18:58
 aaissaoui.FHP029908.mapdrives.txt                           28/08/2014 14:41:09
 aaissaoui.FHP030685.mapdrives.txt                           15/11/2014 18:27:55
 aaissaoui.FHP048791.mapdrives.txt                           12/09/2014 10:22:19
 aaissaoui.FHP048963.mapdrives.txt                           06/11/2014 09:55:00

我只想获得每次登录的最新文件(例如AABASSi是登录名)

如果我尝试sort-object lastwritetime -descending和sort-object -property name -unique,则会出现问题,因为例如,AAISSAOUI 28/08/2014 14:41:09不是最新文件:

 Get-ChildItem | select @{l="name";e={($_.name.split(".")[0]).ToUpper()}},lastwritetime | Sort-Object lastwritetime -Descending | Sort-Object -Property name -Unique

 name                                                        LastWriteTime
 ----                                                        -------------
AABASSI                                                     26/11/2014 18:08:48
AABOUD                                                      30/12/2014    15:05:04
AACCUS                                                      25/07/2014 08:16:19
AADRIENVASSE                                                02/10/2015 14:18:15
AAGOSTINI                                                   05/03/2015 13:38:07
AAGRICOLE                                                   25/03/2015 12:32:06
AAIAD                                                       09/06/2015 09:18:58
AAISSAOUI                                                   28/08/2014 14:41:09

对我的问题来说,这是一个简单的解决方案吗?

1 个答案:

答案 0 :(得分:2)

您遇到排序问题的原因是您按实际需要的属性对其进行排序,然后再使用其他属性(名称)对其进行排序。而是通过“。”之前的字符串对对象进行分组,并仅从每个组中选择最新的对象。 @ PetSerAl的方法很棒:

   Get-ChildItem | Group-Object {$_.Name.Split(".")[0]} | % {
       $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -First 1
    }