如何使用 PowerShell 中的 Get-ChildItem 檢索特定檔案資訊?


當給 Get-ChildItem cmdlet 提供專案(檔案)路徑時,它會提取檔案資訊,如名稱、LastWriteTime、大小等。

示例

Get-ChildItem -Path D:\Temp\style.css

輸出

Directory: D:\Temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       08-12-2017     10:16            393 style.css

命令

若要獲取檔案的完整屬性,則需要使用 fl * (Format-List *) 管道命令。

Get-ChildItem -Path D:\Temp\style.css | fl *

輸出

PSPath            : Microsoft.PowerShell.Core\FileSystem::D:\Temp\style.css
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::D:\Temp
PSChildName       : style.css
PSDrive           : D
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             D:\Temp\style.css
                    InternalName:
                    OriginalFilename:
                    FileVersion:
                    FileDescription:
                    Product:
                    ProductVersion:
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:

BaseName          : style
Target            : {}
LinkType          :
Name              : style.css
Length            : 393
DirectoryName     : D:\Temp
Directory         : D:\Temp
IsReadOnly        : False
Exists            : True
FullName          : D:\Temp\style.css
Extension         : .css
CreationTime      : 08-12-2017 10:02:17
CreationTimeUtc   : 08-12-2017 04:32:17
LastAccessTime    : 08-12-2017 10:02:17
LastAccessTimeUtc : 08-12-2017 04:32:17
LastWriteTime     : 08-12-2017 10:16:26
LastWriteTimeUtc  : 08-12-2017 04:46:26
Attributes        : Archive

命令

你可以透過管道 Select-Object 引數來獲取特定屬性。從上述示例中,我們將顯示檔名、屬性、副檔名、建立時間、最後訪問時間和最後寫入時間。

Get-ChildItem D:\Temp\style.css | Select Name, Extension, CreationTime, LastAccessTime, LastWriteTime

輸出

Name           : style.css
Extension      : .css
CreationTime   : 08-12-2017 10:02:17
LastAccessTime : 08-12-2017 10:02:17
LastWriteTime  : 08-12-2017 10:16:26

同樣,你可以在同一個命令中檢索多個檔案資訊。每個檔名需要用逗號 (,) 分隔。

PS D:\Temp> Get-ChildItem .\style.css, .\cars.xml

更新於:2020-01-22

2K+ 檢視

開啟您的 職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.