Powershell - 使用 Get-ChildItem 获取简单的文件列表(包含其路径)

问题描述 投票:0回答:3

我想要一个 Powershell 脚本,它输出通过递归找到的文件,并在一定时间后修改日期。

目前,它是这样的单个命令;

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } 

但是,输出并不容易使用,因为我得到这样的结果:

            Directory: C:\Test
            Mode                LastWriteTime         Length Name
            ----                -------------         ------ ----
            ------        02/01/2020    15:58       69005864 x.exe
            ------        02/01/2020    15:17         144182 y.pptx
            ------        02/01/2020    17:34       57452572 z.exe

我希望我的输出是:

C:\Test\x.exe
C:\Test\y.pptx
C:\Test\z.exe

以及所有其他文件夹中的文件。一个人会怎样做呢?

windows powershell file search scripting
3个回答
2
投票

这是一个很好的问题,特别是来自命令提示符背景,您可以轻松地键入

dir /s/b
并获得类似的结果。这是我通常做的事情:

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } | ForEach-Object { $_.FullName }

实际上,我使用别名来减少打字:

dir -r | ?{ $_.LastWriteTime -ge "01/01/2020 19:57:00" } | %{ $_.FullName }

或者如果您想按照@Ash的建议使用

Select-Object

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } | Select-Object -Property FullName | Format-Table -HideTableHeaders -AutoSize

有别名:

dir -r | ?{ $_.LastWriteTime -ge "01/01/2020 19:57:00" } | Select-Object FullName | ft -a -h

后者只是有点不太简洁。


0
投票

powershell 的输出是一种幻觉。它实际上是输出具有属性组的对象。某些对象类型具有控制其外观的格式文件。查看真实属性列表的一种方法是将其通过管道传输到

format-list *
或简称
fl *
。您可以在此处看到
FullName
属性,显示文件的完整路径。即使在此列表中,
VersionInfo
属性也从某些格式文件中获取额外的格式。

get-childitem foo | fl *


PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\users\js\foo\foo
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\users\js\foo
PSChildName       : foo
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             C:\users\js\foo\foo
                    InternalName:
                    OriginalFilename:
                    FileVersion:
                    FileDescription:
                    Product:
                    ProductVersion:
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:

BaseName          : foo
Target            : {}
LinkType          :
Name              : foo
Length            : 4
DirectoryName     : C:\users\js\foo
Directory         : C:\users\js\foo
IsReadOnly        : False
Exists            : True
FullName          : C:\users\js\foo\foo
Extension         :
CreationTime      : 1/2/2020 1:44:37 PM
CreationTimeUtc   : 1/2/2020 6:44:37 PM
LastAccessTime    : 1/2/2020 1:44:37 PM
LastAccessTimeUtc : 1/2/2020 6:44:37 PM
LastWriteTime     : 1/2/2020 1:44:37 PM
LastWriteTimeUtc  : 1/2/2020 6:44:37 PM
Attributes        : Archive

另一个例子。通常标题与属性名称匹配:

dir | select mode,lastwritetime,length,name

Mode   LastWriteTime         length Name
----   -------------         ------ ----
d----- 1/3/2020 11:35:44 AM         foo2
-a---- 1/2/2020 3:53:32 PM   4      0.txt
-a---- 1/2/2020 3:53:37 PM   4      9.txt
-a---- 12/24/2019 1:45:07 PM 42     file.json
-a---- 12/23/2019 2:26:40 PM 63     file.json~
-a---- 1/2/2020 1:44:37 PM   4      foo
-a---- 1/2/2020 3:54:14 PM   50     listnum.ps1
-a---- 1/2/2020 3:53:15 PM   41     listnum.ps1~

0
投票

gci -File -Recurse -Name

gci
GetChild-Item

的别名

这将为您提供您想要的输出。

© www.soinside.com 2019 - 2024. All rights reserved.