windows下的一些命令汇总

1、Get-ChildItem

Get-ChildItem 是 PowerShell 中最常用的命令之一,用于获取指定位置(文件系统、注册表、证书存储等)中的子项(文件、文件夹、注册表键等)。它类似于传统 cmd 中的 dir 命令或 Linux 中的 ls,但功能更强大。

基本语法

Get-ChildItem \[-Path <string\[\]>] \[-Filter <string>] -Recurse -Include \>] -Exclude \>] -Name -Directory -File -Hidden -ReadOnly -System -Depth \ 其他参数

参数 说明 示例
-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

相关推荐
无奈笑天下1 天前
Wine适配Windows程序常见故障标准处理流程
linux·运维·服务器·windows
广东程序猿小展1 天前
PotPlayer 播放 4K 视频卡顿怎么办(硬解与渲染设置)
windows·工具·软件
xx z2 天前
Radmin LAN 怎么联机(虚拟局域网建房与加入教程)
windows·工具·软件
广东程序猿小展2 天前
谷歌相机配置文件怎么导入(GCam 配置导入与不生效排查)
windows·工具·软件
李小白杂货铺3 天前
Windows 桌面 GUI 原型快速开发技术选型指南(Mac 开发 + Windows 交付场景)
windows·原型·gui·原型开发·技术选型·快速开发·技术路线
承渊政道3 天前
magic-resume简历工具教程:Windows部署、在线编辑与远程访问
windows·远程访问·在线编辑·简历制作·工具教程·magic-resume
观察员4 天前
告别频繁锁屏:5行 PowerShell 脚本实现 Windows 自动防休眠
windows·命令行
Funlang4 天前
在 Windows 95 上写前端
前端·javascript·windows
一切皆有可能!!4 天前
【SoduWork测评】本机跑源码,找到并修了7个Bug
windows·开源·bug·ai编程·soduwork