1、Get-ChildItem
Get-ChildItem 是 PowerShell 中最常用的命令之一,用于获取指定位置(文件系统、注册表、证书存储等)中的子项(文件、文件夹、注册表键等)。它类似于传统 cmd 中的 dir 命令或 Linux 中的 ls,但功能更强大。
基本语法
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Recurse] [-Include <string[]>] [-Exclude <string[]>] [-Name] [-Directory] [-File] [-Hidden] [-ReadOnly] [-System] [-Depth <uint32>] [其他参数]
| 参数 | 说明 | 示例 |
|---|---|---|
-Path |
指定要获取的路径(支持多个,支持通配符 *、?)。默认当前目录。 |
Get-ChildItem -Path C:\Windows\System32\drivers\*.sys |
-Filter |
更高效的过滤,在提供程序层执行,支持通配符但不支持正则。 | Get-ChildItem -Filter *.log |
-Include |
包含特定模式的文件/目录(在获取后过滤,多个模式用逗号分隔)。 | -Include *.txt, *.csv |
-Exclude |
排除特定模式的文件/目录。 | -Exclude temp* |
-Recurse |
递归进入所有子目录。 | -Recurse |
-Depth |
限制递归深度(PowerShell 5.0+)。 | -Depth 2 |
-Name |
只返回名称(字符串),不返回对象。 | Get-ChildItem -Name |
-Directory |
只获取目录。 | -Directory |
-File |
只获取文件。 | -File |
-Hidden |
包含隐藏项。 | -Hidden |
-ReadOnly |
只读项。 | -ReadOnly |
-System |
系统项。 | -System |
-Force |
强制获取本来无法访问的项(如隐藏、系统文件)。 | -Force |
-LiteralPath |
与 -Path 类似,但不支持通配符,用于路径中恰好包含特殊字符时。 |
-LiteralPath 'C:\Program Files' |
通配符与过滤
Get-ChildItem *.ps1 # 当前目录下所有 .ps1 文件
Get-ChildItem -Include *.txt, *.log -Recurse # 递归查找所有 .txt 和 .log
Get-ChildItem -Exclude *test* -Recurse # 排除名字包含 test 的项
属性筛选
Get-ChildItem -File -Hidden # 隐藏文件
Get-ChildItem -Directory -ReadOnly # 只读文件夹
其他
bash
Get-ChildItem -Recurse -Depth 1 # 递归查找当前目录及其下一级子目录
Get-ChildItem -Name # 仅显示文件名,不返回完整对象信息
Get-ChildItem -LiteralPath 'C:\logs\[2024]' # 路径中有特殊字符时使用
与其他命令结合(管道)
与 Select-String 组合(搜索php文件总所有包含error的内容)
bash
Get-ChildItem -Recurse -Include *.php | Select-String "error"
删除所有 .tmp 文件
bash
Get-ChildItem -Recurse -Filter *.tmp | Remove-Item
获取超过30天未修改的文件
bash
$date = (Get-Date).AddDays(-30)
Get-ChildItem -Recurse -File | Where-Object { $_.LastWriteTime -lt $date }
统计各扩展名的文件数量
bash
Get-ChildItem -Recurse -File | Group-Object Extension | Select-Object Name, Count
2、Select-String
Select-String 是 PowerShell 中用于在文本或文件内容中搜索匹配字符串(正则表达式)的命令。它类似于 Unix 中的 grep 或 Windows 的 findstr,但功能更强大,输出对象化,便于进一步处理。
2.1语法结构
bash
Select-String
[-Pattern] <string[]>
[-Path] <string[]>
[-SimpleMatch]
[-CaseSensitive]
[-NotMatch]
[-AllMatches]
[-Context <int[]>]
[-Encoding <Encoding>]
[-List]
[-Quiet]
[其他参数]
2.2 参数详解
| 参数 | 说明 | 示例 |
|---|---|---|
-Pattern |
必需。要搜索的模式(支持正则表达式)。多个模式用数组提供。 | -Pattern "error", "warning" |
-Path |
指定要搜索的文件路径(支持通配符 *、?)。 |
-Path "C:\logs\*.log" |
-SimpleMatch |
将模式视为普通文本,不使用正则。提高性能并避免转义。 | -SimpleMatch -Pattern "a+b=c" |
-CaseSensitive |
区分大小写。默认不区分。 | -CaseSensitive |
-NotMatch |
返回不匹配的行。 | -NotMatch |
-AllMatches |
在同一行中返回多个匹配(默认每行只返回第一个)。 | -AllMatches |
-Context <int[]> |
返回匹配行的上下文(前后行数)。可写 -Context 2,1(前2后1)。 |
-Context 2,2 |
-Encoding |
指定文件编码(如 UTF8、Default)。 | -Encoding UTF8 |
-List |
只返回每个文件的第一个匹配。 | -List |
-Quiet |
返回布尔值(是否找到匹配),不返回匹配内容。 | -Quiet |
-Raw |
直接输出匹配的字符串(不包装成 MatchInfo 对象)。 | -Raw |
2.3 输入方式
Select-String 可以从两种源接收输入:
方式一:通过管道传输字符串
bash
"hello world", "goodbye world" | Select-String "hello"
方式二:使用 -Path 参数直接搜索文件
bash
Select-String -Path "*.txt" -Pattern "error"
也可以混合使用:
bash
Get-Content log.txt | Select-String "error"
2.4 常用示例
示例1:搜索单个文件
bash
Select-String -Path "C:\test\app.log" -Pattern "Exception"
示例2:搜索多个文件(通配符)
bash
Select-String -Path "C:\logs\*.log" -Pattern "Failed"
示例3:递归搜索目录(结合 Get-ChildItem)
bash
Get-ChildItem -Recurse -Include *.php | Select-String "openssl_random_pseudo_bytes"
示例4:简单匹配(不使用正则)
bash
Select-String -Path "*.txt" -Pattern "a[0-9]" -SimpleMatch
# 此时会搜索字面字符串 "a[0-9]",而不是正则
示例5:区分大小写
bash
Select-String -Path "*.ps1" -Pattern "write-host" -CaseSensitive
示例6:反向匹配(不包含的行)
bash
Get-Content config.ini | Select-String ";" -NotMatch # 输出不含分号的行
示例7:显示匹配行上下文
bash
Select-String -Path "error.log" -Pattern "Fatal" -Context 2,3
# 输出匹配行及其前2行、后3行
示例8:只返回是否匹配(静默模式)
bash
if (Select-String -Path "*.txt" -Pattern "secret" -Quiet) {
Write-Host "发现敏感词"
}
示例9:每文件只返回第一个匹配
bash
Select-String -Path "*.log" -Pattern "Error" -List
示例10:一行中匹配多个结果
bash
$line = "a1 b2 c3 d4"
$line | Select-String "\d+" -AllMatches
# 返回多个 MatchInfo 对象,每个数字一个
综合使用:
Get-ChildItem -Recurse -Include *.php | Select-String "error"
命令详解:
-
Get-ChildItem -Recurse:递归获取当前目录下所有子目录中的文件。 -
-Include *.php:只筛选出扩展名为.php的文件。 -
Select-String "error":在这些文件中搜索包含该字符串的行
该命令会自动遍历所有子文件夹,查找包含error的文件,如果只想要遍历当前目录,去掉-Recurse