用 Get-ChildItem 递归搜索文件
一行 PowerShell 在所有子文件夹中按文件名查找文件,并忽略无权限的目录。
运行 `Get-ChildItem -Path <目录> -Recurse -Filter "*.log" -ErrorAction SilentlyContinue`,在所有子文件夹中查找匹配文件。
01命令
命令
$ Get-ChildItem -Path "$env:USERPROFILE" -Recurse -Filter "*.txt" -ErrorAction SilentlyContinue02作用说明
-Recurse 让搜索深入所有子目录;-Filter 在提供程序层面快速匹配文件名,对单一模式来说比 -Include 更快。-ErrorAction SilentlyContinue 会屏蔽递归系统目录时必然出现的“拒绝访问”提示。输出是 FileInfo 对象,可以直接接 Sort-Object Length 或 Where-Object LastWriteTime 继续筛选。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Get-ChildItem (Microsoft.PowerShell.Management)依据: Documents Get-ChildItem with -Recurse for nested search, -Filter for provider-level filename patterns and -ErrorAction handling.
最后验证: 2026-09-17 · 分类: 文件与文件夹