#PowerShell 的 Get-ChildItem 命令
Get-ChildItem
[[-Path] <String[]>]
[[-Filter] <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Depth <UInt32>]
[-Force]
[-Name]
[<CommonParameters>]
Get-ChildItem
[[-Filter] <String>]
-LiteralPath <String[]>
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Depth <UInt32>]
[-Force]
[-Name]
[<CommonParameters>]
功能
获取一个或多个位置中的项;如果目标是容器,则获取容器内的子项。
类型
Cmdlet,属于 Microsoft.PowerShell.Management。
参数
[-Path] <String[]>- 要列出子项的位置,默认为当前目录;支持通配符-LiteralPath <String[]>- 按字面值解释路径,不将任何字符视为通配符-Filter <String>- 由提供程序执行的筛选器,比-Include更高效;仅支持*和?-Include <String[]>- 只包含匹配的项;通常需要路径以*结尾,或配合-Recurse-Exclude <String[]>- 排除匹配的项;在包含筛选之后应用-Recurse- 递归获取所有子容器中的项-Depth <UInt32>- 限制递归的目录层级,控制要包含的子目录层数-Force- 显示隐藏项和系统项等默认不可见的项-File- 只返回文件;仅适用于 FileSystem 提供程序-Directory- 只返回目录;仅适用于 FileSystem 提供程序
完整参数可使用
Get-Help Get-ChildItem -Full查看。
#示例
列出目录内容
> Get-ChildItem -Path .
- 不带参数时等同于列出当前目录内容。
- 默认显示模式、最后修改时间、长度和名称。
递归查找文件
> Get-ChildItem -Path . -Filter *.ps1 -Recurse
-Filter由 FileSystem 提供程序执行,通常比-Include更快。- 使用
-Depth可以限制递归层级。
按模式包含或排除
> Get-ChildItem -Path .\* -Include *.txt, *.md
> Get-ChildItem -Path .\* -Exclude *.log
- 使用
-Include时,通常需要在路径末尾加上*,否则可能没有输出。
处理含特殊字符的路径
> Get-ChildItem -LiteralPath './files[1]'
- 方括号会被通配符解析,使用
-LiteralPath可按字面值访问。