Get-ChildItem
获取一个或多个指定位置中的项和子项。
语法
Get-ChildItem
[[-Path] <string[]>]
[[-Filter] <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Depth <uint32>]
[-Force]
[-Name]
[-UseTransaction]
[-Attributes <FlagsExpression[FileAttributes]>]
[-Directory]
[-File]
[-Hidden]
[-ReadOnly]
[-System]
[<CommonParameters>]
Get-ChildItem
[[-Filter] <string>]
-LiteralPath <string[]>
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse]
[-Depth <uint32>]
[-Force]
[-Name]
[-UseTransaction]
[-Attributes <FlagsExpression[FileAttributes]>]
[-Directory]
[-File]
[-Hidden]
[-ReadOnly]
[-System]
[<CommonParameters>]
说明
Get-ChildItem
cmdlet 获取一个或多个指定位置中的项。 如果项是容器,它将获取容器内的项,称为子项。 可以使用 Recurse 参数获取所有子容器中的项,并使用 Depth 参数来限制递归级别数。
Get-ChildItem
不显示空目录。 当 Get-ChildItem
命令包含 深度 或 递归 参数时,输出中不包含空目录。
位置由 PowerShell 提供程序向 Get-ChildItem
公开。 位置可以是文件系统目录、注册表配置单元或证书存储。 有关详细信息,请参阅 about_Providers。
示例
示例 1:从文件系统目录获取子项
此示例从文件系统目录中获取子项。 将显示文件名和子目录名称。 对于空位置,该命令不返回任何输出并返回到 PowerShell 提示符。
Get-ChildItem
cmdlet 使用 Path 参数来指定目录 C:\Test
。
Get-ChildItem
PowerShell 控制台中显示文件和目录。
Get-ChildItem -Path C:\Test
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/15/2019 08:29 Logs
-a---- 2/13/2019 08:55 26 anotherfile.txt
-a---- 2/12/2019 15:40 118014 Command.txt
-a---- 2/1/2019 08:43 183 CreateTestFile.ps1
-ar--- 2/12/2019 14:31 27 ReadOnlyFile.txt
默认情况下,Get-ChildItem
列出模式(属性)、LastWriteTime、文件大小(长度),以及项 名称。
Mode 属性中的字母可以插在一起,如下所示:
-
l
(链接) -
d
(目录) -
a
(存档) -
r
(只读) -
h
(隐藏) -
s
(系统)。
有关模式标志的详细信息,请参阅 about_Filesystem_Provider。
示例 2:获取目录中的子项名称
此命令仅列出目录中项的名称。
Get-ChildItem
cmdlet 使用 Path 参数来指定目录 C:\Test
。
Name 参数仅返回指定路径中的文件或目录名称。
Get-ChildItem -Path C:\Test -Name
Logs
anotherfile.txt
Command.txt
CreateTestFile.ps1
ReadOnlyFile.txt
示例 3:获取当前目录和子目录中的子项
此示例显示位于当前目录及其子目录中的 .txt 文件。
Get-ChildItem -Path C:\Test\*.txt -Recurse -Force
Directory: C:\Test\Logs\Adirectory
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/12/2019 16:16 20 Afile4.txt
-a-h-- 2/12/2019 15:52 22 hiddenfile.txt
-a---- 2/13/2019 13:26 20 LogFile4.txt
Directory: C:\Test\Logs\Backup
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/12/2019 16:16 20 ATextFile.txt
-a---- 2/12/2019 15:50 20 LogFile3.txt
Directory: C:\Test\Logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/12/2019 16:16 20 Afile.txt
-a-h-- 2/12/2019 15:52 22 hiddenfile.txt
-a---- 2/13/2019 13:26 20 LogFile1.txt
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/13/2019 08:55 26 anotherfile.txt
-a---- 2/12/2019 15:40 118014 Command.txt
-a-h-- 2/12/2019 15:52 22 hiddenfile.txt
-ar--- 2/12/2019 14:31 27 ReadOnlyFile.txt
Get-ChildItem
cmdlet 使用 Path 参数来指定 C:\Test\*.txt
。
路径 使用星号(*
)通配符指定文件扩展名为 .txt的所有文件。
Recurse 参数搜索其子目录的 Path 目录,如 目录: 标题所示。
Force 参数显示具有 h模式的隐藏文件,例如 hiddenfile.txt。
示例 4:使用 Include 参数获取子项
在此示例中,Get-ChildItem
使用 Include 参数从 Path 参数指定的目录中查找特定项。
# When using the -Include parameter, if you do not include an asterisk in the path
# the command returns no output.
Get-ChildItem -Path C:\Test\ -Include *.txt
Get-ChildItem -Path C:\Test\* -Include *.txt
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/13/2019 08:55 26 anotherfile.txt
-a---- 2/12/2019 15:40 118014 Command.txt
-ar--- 2/12/2019 14:31 27 ReadOnlyFile.txt
Get-ChildItem
cmdlet 使用 Path 参数指定目录 C:\Test。
Path 参数包括尾随星号(*
)通配符,用于指定目录的内容。
Include 参数使用星号(*
)通配符指定文件扩展名为 .txt的所有文件。
使用 Include 参数时,Path 参数需要尾随星号(*
)通配符来指定目录的内容。 例如,-Path C:\Test\*
。
- 如果将 Recurse 参数添加到命令,Path 参数中的尾随星号 (
*
) 是可选的。 Recurse 参数从 Path 目录及其子目录中获取项。 例如,-Path C:\Test\ -Recurse -Include *.txt
- 如果 Path 参数中不包含尾随星号(
*
),该命令不会返回任何输出并返回到 PowerShell 提示符。 例如,-Path C:\Test\
。
示例 5:使用 Exclude 参数获取子项
该示例的输出显示目录 C:\Test\Logs的内容。 输出是使用 排除 和 递归 参数的其他命令的引用。
Get-ChildItem -Path C:\Test\Logs
Directory: C:\Test\Logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/15/2019 13:21 Adirectory
d----- 2/15/2019 08:28 AnEmptyDirectory
d----- 2/15/2019 13:21 Backup
-a---- 2/12/2019 16:16 20 Afile.txt
-a---- 2/13/2019 13:26 20 LogFile1.txt
-a---- 2/12/2019 16:24 23 systemlog1.log
Get-ChildItem -Path C:\Test\Logs\* -Exclude A*
Directory: C:\Test\Logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/15/2019 13:21 Backup
-a---- 2/13/2019 13:26 20 LogFile1.txt
-a---- 2/12/2019 16:24 23 systemlog1.log
Get-ChildItem
cmdlet 使用 Path 参数来指定目录 C:\Test\Logs
。
Exclude 参数使用星号(*
)通配符指定从输出中排除 A 或 开头的任何文件或目录。
使用 Exclude 参数时,Path 参数中的尾随星号 (*
) 是可选的。 例如,-Path C:\Test\Logs
或 -Path C:\Test\Logs\*
。
- 如果 Path 参数中不包含尾随星号(
*
),则会显示 Path 参数的内容。 例外情况是与 排除 参数值匹配的文件名或子目录名称。 - 如果 Path 参数中包含尾随星号(
*
),则命令 将 递归到 Path 参数的子目录中。 例外情况是与 排除 参数值匹配的文件名或子目录名称。 - 如果将 Recurse 参数添加到命令,则递归输出与 Path 参数是否包含尾随星号(
*
)相同。
示例 6:从注册表配置单元中获取注册表项
此命令从 HKEY_LOCAL_MACHINE\HARDWARE
注册表项中获取所有注册表项。
Get-ChildItem
使用 Path 参数指定注册表项 HKLM:\HARDWARE
。 Hive 的路径和顶级注册表项显示在 PowerShell 控制台中。
有关详细信息,请参阅 about_Registry_Provider。
Get-ChildItem -Path HKLM:\HARDWARE
Hive: HKEY_LOCAL_MACHINE\HARDWARE
Name Property
---- --------
ACPI
DESCRIPTION
DEVICEMAP
RESOURCEMAP
UEFI
Get-ChildItem -Path HKLM:\HARDWARE -Exclude D*
Hive: HKEY_LOCAL_MACHINE\HARDWARE
Name Property
---- --------
ACPI
RESOURCEMAP
第一个命令显示 HKLM:\HARDWARE
注册表项的内容。
Exclude 参数指示 Get-ChildItem
不返回以 D*
开头的任何子项。 目前,Exclude 参数仅适用于子项,不适用于项属性。
示例 7:获取具有代码签名机构的所有证书
此命令获取 PowerShell 证书中的每个证书: 具有代码签名机构的驱动器。
Get-ChildItem
cmdlet 使用 Path 参数来指定 证书: 提供程序。
Recurse 参数搜索由 Path 及其子目录指定的目录。
CodeSigningCert 参数仅获取具有代码签名机构的证书。
Get-ChildItem -Path Cert:\* -Recurse -CodeSigningCert
有关证书提供程序和证书:驱动器的详细信息,请参阅 about_Certificate_Provider。
示例 8:使用 Depth 参数获取项
此命令显示目录及其子目录中的项。 Depth 参数确定要包括在递归中的子目录级别数。 从输出中排除空目录。
Get-ChildItem -Path C:\Parent -Depth 2
Directory: C:\Parent
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/14/2019 10:24 SubDir_Level1
-a---- 2/13/2019 08:55 26 file.txt
Directory: C:\Parent\SubDir_Level1
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/14/2019 10:24 SubDir_Level2
-a---- 2/13/2019 08:55 26 file.txt
Directory: C:\Parent\SubDir_Level1\SubDir_Level2
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/14/2019 10:22 SubDir_Level3
-a---- 2/13/2019 08:55 26 file.txt
Get-ChildItem
cmdlet 使用 Path 参数来指定 C:\Parent。
Depth 参数指定两个递归级别。
Get-ChildItem
显示由 Path 参数和子目录的两个级别指定的目录的内容。
参数
-Attributes
获取具有指定属性的文件和文件夹。 此参数支持所有属性,并允许指定属性的复杂组合。
例如,若要获取加密或压缩的非系统文件(而不是目录),请键入:
Get-ChildItem -Attributes !Directory+!System+Encrypted, !Directory+!System+Compressed
若要查找具有常用属性的文件和文件夹,请使用 Attributes 参数。 或者,Directory、File、Hidden、ReadOnly和 System的参数。
Attributes 参数支持以下属性:
- 存档
- 压缩
- 设备
- Directory
- 加密
- 隐藏
- IntegrityStream
- 普通
- NoScrubData
- NotContentIndexed
- 脱机
- ReadOnly
- ReparsePoint
- 稀疏File
- 系统
- 临时
有关这些属性的说明,请参阅 FileAttributes 枚举。
若要组合属性,请使用以下运算符:
-
!
(NOT) -
+
(AND) -
,
(OR)
请勿在运算符及其属性之间使用空格。 逗号后接受空格。
对于常见属性,请使用以下缩写:
-
D
(目录) -
H
(隐藏) -
R
(只读) -
S
(系统)
类型: | FlagsExpression<T>[FileAttributes] |
接受的值: | Archive, Compressed, Device, Directory, Encrypted, Hidden, IntegrityStream, Normal, NoScrubData, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, Temporary |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Depth
此参数已添加到 PowerShell 5.0 中,使你能够控制递归深度。 默认情况下,Get-ChildItem
显示父目录的内容。
Depth 参数确定递归中包含的子目录级别数,并显示内容。
例如,Depth 2
包括 Path 参数的目录、第一级子目录和第二级子目录。 默认情况下,输出中包含目录名称和文件名。
注意
在 PowerShell 或 cmd.exe的 Windows 计算机上,可以使用 tree.com 命令显示目录结构的图形视图。
类型: | UInt32 |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Directory
若要获取目录列表,请使用 Directory 参数或具有 Directory 属性的 Attributes 参数。 可以将 Recurse 参数与 Directory配合使用。
类型: | SwitchParameter |
别名: | ad, d |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Exclude
指定此 cmdlet 从操作中排除的属性或属性作为字符串数组。
此参数的值限定 Path 参数。 输入路径元素或模式,例如 *.txt
或 A*
。 接受通配符。
Path 参数中的尾随星号(*
)是可选的。 例如,-Path C:\Test\Logs
或 -Path C:\Test\Logs\*
。 如果包含尾随星号(*
),该命令将递归到 Path 参数的子目录中。 如果没有星号(*
),将显示 Path 参数的内容。 示例 5 和 Notes 部分包含更多详细信息。
类型: | String[] |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | True |
-File
若要获取文件列表,请使用 File 参数。 可以将 Recurse 参数与 File一起使用。
类型: | SwitchParameter |
别名: | af |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Filter
指定用于限定 Path 参数的筛选器。
FileSystem 提供程序是唯一支持使用筛选器的已安装 PowerShell 提供程序。 筛选器比其他参数更有效,因为提供程序在 cmdlet 获取对象时应用它们,而不是在检索对象后让 PowerShell 筛选对象。 筛选器字符串将传递给 .NET API 以枚举文件。 该 API 仅支持 *
和 ?
通配符。
类型: | String |
Position: | 1 |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | True |
-Force
允许该 cmdlet 获取用户无法访问的项,例如隐藏文件或系统文件。 Force 参数不会替代安全限制。 实现因提供程序而异。 有关详细信息,请参阅 about_Providers。
类型: | SwitchParameter |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Hidden
若要仅获取隐藏的项,请使用 Hidden 参数或具有 Hidden 属性的 Attributes 参数。 默认情况下,Get-ChildItem
不显示隐藏的项。 使用 Force 参数获取隐藏项。
类型: | SwitchParameter |
别名: | ah, h |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Include
指定此 cmdlet 包含在操作中的项(作为字符串数组)。 此参数的值限定 Path 参数。 输入路径元素或模式,例如 "*.txt"
。 允许使用通配符。 仅当命令包含项的内容(如 C:\Windows\*
)时,Include 参数才有效,其中通配符指定 C:\Windows
目录的内容。
类型: | String[] |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | True |
-LiteralPath
指定一个或多个位置的路径。 LiteralPath 的值与类型化完全相同。 不会将任何字符解释为通配符。 如果路径包含转义字符,请将它括在单引号中。 单引号告知 PowerShell 不要将任何字符解释为转义序列。
有关详细信息,请参阅 about_Quoting_Rules。
类型: | String[] |
别名: | PSPath |
Position: | Named |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Name
仅获取位置中项的名称。 输出是一个字符串对象,可以将管道向下发送到其他命令。 允许通配符。
类型: | SwitchParameter |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | True |
-Path
指定一个或多个位置的路径。 接受通配符。 默认位置为当前目录(.
)。
类型: | String[] |
Position: | 0 |
默认值: | Current directory |
必需: | False |
接受管道输入: | True |
接受通配符: | True |
-ReadOnly
若要仅获取只读项,请使用 ReadOnly 参数或 Attributes 参数 ReadOnly 属性。
类型: | SwitchParameter |
别名: | ar |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Recurse
获取指定位置和位置的所有子项中的项。
类型: | SwitchParameter |
别名: | s |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-System
仅获取系统文件和目录。 若要仅获取系统文件和文件夹,请使用 System 参数或 属性 System 属性 参数。
类型: | SwitchParameter |
别名: | as |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-UseTransaction
在活动事务中包含该命令。 此参数仅在事务正在进行时有效。 有关详细信息,请参阅 about_Transactions。
类型: | SwitchParameter |
别名: | usetx |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
可以通过管道传递包含 Get-ChildItem
路径的字符串。
输出
Get-ChildItem
返回的对象的类型由提供程序驱动器路径中的对象确定。
如果使用 Name 参数,Get-ChildItem
将对象名称作为字符串返回。
备注
- 可以通过其内置别名、
ls
、dir
和gci
来引用Get-ChildItem
。 有关详细信息,请参阅 about_Aliases。 - 默认情况下,
Get-ChildItem
不会获取隐藏项。 若要获取隐藏项,请使用 Force 参数。 -
Get-ChildItem
cmdlet 旨在处理任何提供程序公开的数据。 若要列出会话中可用的提供程序,请键入Get-PSProvider
。 有关详细信息,请参阅 about_Providers。