PowerShell · 文件与文本

Get-ChildItem 命令怎么用?列出文件和文件夹,含搜索(PowerShell 参数与示例)

Windows 11 / 10普通 PowerShell 窗口即可更新于

Get-ChildItem列出文件和文件夹(含搜索)

语法:Get-ChildItem [路径] [-Recurse] [-Filter 条件] [-File] [-Directory] [-Force]

最常用:Get-ChildItem D:\ -Recurse -Filter *.mp4 -ErrorAction SilentlyContinue —— 在 D 盘查找所有 mp4 文件,忽略没有权限的文件夹

怎么打开 PowerShell

右键开始按钮,选择“终端”或“Windows PowerShell”。命令不区分大小写,按 Tab 键可以自动补全命令和参数名。

常用参数

参数作用
-Recurse包括所有子文件夹
-Filter "*.pdf"按名称筛选,速度快
-File / -Directory只列文件 / 只列文件夹
-Force包括隐藏和系统文件
-Depth N限制递归层数

使用示例

Get-ChildItem D:\ -Recurse -Filter *.mp4 -ErrorAction SilentlyContinue

在 D 盘查找所有 mp4 文件,忽略没有权限的文件夹

Get-ChildItem C:\Users\$env:USERNAME\Downloads -File | Sort-Object LastWriteTime -Descending

按修改时间列出下载文件夹中的文件

(Get-ChildItem D:\照片 -Recurse -File | Measure-Object Length -Sum).Sum / 1GB

统计文件夹总大小(GB)

示例中的盘符、路径和名称要换成你自己的。删除、修改类命令可以先在末尾加 -WhatIf 预览会发生什么。

注意事项

  • 别名 dir、ls、gci 都是它。

对应的 CMD 命令

在命令提示符(cmd)中实现类似功能的命令是 dir。PowerShell 中也能直接运行大部分 CMD 命令。