002:windows命令速查手册

CMD vs PowerShell 对比

特性 CMD (命令提示符) PowerShell
推出时间 1981 (MS-DOS) 2006
文件扩展名 .bat, .cmd .ps1
命令语法 简单命令 面向对象的cmdlet
管道传递 文本 对象
别名支持 有限 强大
脚本能力 基础 高级(类似编程语言)
系统集成 基础 深度集成 .NET
适用场景 简单任务、传统脚本 复杂自动化、系统管理
默认终端 Windows 10 及之前 Windows 11 默认

CMD 常用命令

常用命令列表

序号 命令 描述 使用示例 兼容性
1 cd 切换目录 cd C:\Users cd .. CMD + PS
2 dir 列出目录内容 dir dir /w dir /s /b CMD + PS
3 cls 清屏 cls CMD + PS
4 copy 复制文件 copy file1.txt file2.txt copy *.txt D:\backup\ CMD + PS
5 del 删除文件 del filename.txt del /s /q *.tmp CMD + PS
6 mkdir / md 创建目录 mkdir newfolder md folder1\folder2 CMD + PS
7 rmdir / rd 删除目录 rmdir foldername rmdir /s /q folder CMD + PS
8 type 显示文件内容 type readme.txt `type file.log more`
9 echo 显示消息 echo Hello World echo %PATH% CMD + PS
10 move 移动/重命名文件 move file.txt D:\backup\ move oldname.txt newname.txt CMD + PS
11 ren / rename 重命名文件 ren old.txt new.txt ren *.txt *.bak CMD + PS
12 ipconfig 网络配置 ipconfig ipconfig /all ipconfig /flushdns CMD + PS
13 ping 测试网络连接 ping google.com ping -t 8.8.8.8 CMD + PS
14 tasklist 显示进程列表 tasklist tasklist /fi "STATUS eq running" CMD + PS
15 taskkill 结束进程 taskkill /IM notepad.exe /F taskkill /PID 1234 CMD + PS
16 find 文本搜索(简单) find "error" logfile.txt `dir find "txt"`
17 findstr 文本搜索(正则) findstr /s /i "pattern" *.log findstr /r "^Error" file.txt CMD 主要
18 xcopy 高级复制 xcopy source dest /s /e xcopy /d /y *.txt backup\ CMD + PS
19 attrib 文件属性 attrib +h file.txt attrib -r -s -h /s /d CMD + PS
20 tree 目录树结构 tree tree /f /a > tree.txt CMD + PS
21 systeminfo 系统信息 systeminfo `systeminfo findstr "OS"`
22 netstat 网络统计 netstat -an netstat -b -o CMD + PS
23 net 网络管理 net user net share net start CMD + PS
24 chkdsk 磁盘检查 chkdsk C: chkdsk /f /r CMD + PS
25 sfc 系统文件检查 sfc /scannow CMD + PS
26 format 格式化磁盘 format D: /fs:ntfs /q CMD + PS
27 diskpart 磁盘分区 diskpart (交互式) CMD + PS
28 robocopy 强大的文件复制 robocopy source dest /mir robocopy /e /z /mt:16 CMD + PS
29 shutdown 关机/重启 shutdown /s /t 0 shutdown /r /f /t 10 CMD + PS
30 gpupdate 更新组策略 gpupdate /force CMD + PS
31 whoami 当前用户信息 whoami whoami /all CMD + PS
32 hostname 计算机名 hostname CMD + PS
33 set 环境变量 set set PATH=%PATH%;C:\bin CMD 主要
34 path 显示/设置路径 path path=%path%;C:\tools CMD 主要
35 start 启动程序 start notepad.exe start . CMD + PS
36 assoc 文件关联 assoc .txt assoc .html=ChromeHTML CMD 主要
37 fc 文件比较 fc file1.txt file2.txt CMD + PS
38 comp 二进制比较 comp file1 file2 /a CMD 主要
39 compact 压缩文件/文件夹 compact /c /s:C:\folder CMD + PS
40 cipher 加密/安全删除 cipher /e folder cipher /w:C:\temp CMD + PS
41 driverquery 驱动程序列表 driverquery /v CMD + PS
42 getmac MAC地址 getmac /v CMD + PS
43 nslookup DNS查询 nslookup google.com nslookup -type=mx domain.com CMD + PS
44 tracert 路由跟踪 tracert google.com CMD + PS
45 route 路由表 route print route add 192.168.1.0 mask 255.255.255.0 192.168.1.1 CMD + PS
46 arp ARP缓存 arp -a arp -d CMD + PS
47 reg 注册表操作 reg query HKLM\Software reg add /export /import CMD + PS
48 sc 服务控制 sc query sc start/stop servicename CMD + PS
49 wmic WMI命令(已弃用) wmic cpu get name wmic process list CMD (已弃用)
50 powercfg 电源配置 powercfg /batteryreport powercfg /energy CMD + PS
51 title 窗口标题 title My Command Window CMD 主要
52 color 控制台颜色 color 0a color /? CMD 主要
53 prompt 提示符格式 prompt $p$g prompt [$t] $p$g CMD 主要
54 time 时间 time time /t CMD + PS
55 date 日期 date date /t CMD + PS
56 ver Windows版本 ver CMD + PS
57 label 卷标 label C: MyDrive CMD + PS
58 vol 显示卷信息 vol C: CMD + PS
59 subst 映射虚拟驱动器 subst Z: C:\LongPath\Folder CMD + PS
60 doskey 命令宏 doskey ll=dir /w doskey /history CMD 主要
61 more 分页显示 `type file.txt more`
62 sort 排序 `dir sort sort file.txt`
63 where 查找可执行文件 where notepad where /r C:\ *.exe CMD + PS
64 replace 替换文件 replace source.txt C:\dest CMD 主要
65 expand 解压CAB文件 expand archive.cab -F:* dest\ CMD + PS

CMD 批处理专用命令

命令 描述 示例
@echo off 关闭命令回显 @echo off
goto 跳转到标签 goto :label
:label 定义标签 :start
if 条件判断 if exist file.txt echo Found
for 循环 for %%f in (*.txt) do echo %%f
call 调用另一个批处理 call script.bat param1
pause 暂停并等待按键 pause
exit 退出 exit /b 0
rem 注释 rem This is a comment
:: 注释(替代) :: Another comment
setlocal / endlocal 变量作用域 setlocal enabledelayedexpansion
%variable% 变量引用 echo %PATH%
%1 - %9 批处理参数 echo %1

PowerShell 常用命令

Cmdlet 命令列表

序号 命令 描述 使用示例
1 dir, ls, gci 列出文件和目录 Get-ChildItem -Recurse ls -Filter *.txt
2 cd, sl 切换目录 Set-Location C:\Users cd ~
3 cls, clear 清屏 Clear-Host
4 cat, type, gc 读取文件内容 Get-Content file.txt gc log.txt -Tail 10
5 sc 写入文件内容 Set-Content file.txt "Hello"
6 copy, cp, cpi 复制文件/文件夹 Copy-Item file.txt D:\backup\ cp -Recurse folder dest
7 del, rm, ri 删除文件/文件夹 Remove-Item file.txt rm -Recurse -Force folder
8 move, mv, mi 移动/重命名 Move-Item file.txt D:\dest\ mv old.txt new.txt
9 ni 创建文件/文件夹 New-Item -ItemType Directory folder ni file.txt -ItemType File
10 - 输出到控制台 Write-Host "Hello" -ForegroundColor Green
11 ps, gps 获取进程 Get-Process ps chrome
12 kill, spps 停止进程 Stop-Process -Name notepad kill -Id 1234 -Force
13 gsv 获取服务 Get-Service gsv -Name wuauserv
14 sasv 启动服务 Start-Service -Name Spooler
15 spsv 停止服务 Stop-Service -Name Spooler
16 - 重启服务 Restart-Service -Name Spooler
17 gcm 查找命令 Get-Command *process* gcm -Module ActiveDirectory
18 help, man 获取帮助 Get-Help Get-Process help about_*
19 gm 查看对象成员 `Get-Process
20 select 选择属性 `Get-Process
21 where, ? 过滤对象 `Get-Process
22 foreach, % 遍历对象 `Get-ChildItem
23 sort 排序 `Get-Process
24 measure 统计/计算 `Get-ChildItem
25 group 分组 `Get-Process
26 - 输出到文件 `Get-Process
27 epcsv 导出CSV `Get-Process
28 ipcsv 导入CSV Import-Csv data.csv
29 - 转换为JSON `Get-Process
30 - 从JSON转换 `Get-Content data.json
31 - 测试路径是否存在 Test-Path C:\file.txt
32 pwd, gl 当前位置 Get-Location
33 iwr, curl, wget HTTP请求 Invoke-WebRequest https://api.github.com curl https://example.com
34 irm REST API调用 Invoke-RestMethod -Uri https://api.github.com/users/user
35 start, saps 启动进程 Start-Process notepad.exe start chrome https://google.com
36 - 网络IP地址 Get-NetIPAddress -AddressFamily IPv4
37 - 网络适配器 Get-NetAdapter
38 tnc 测试网络连接 Test-NetConnection google.com -Port 443
39 - 获取事件日志 Get-EventLog -LogName System -Newest 10
40 - 计算机信息 Get-ComputerInfo
41 gi 获取项目 Get-Item C:\file.txt
42 gp 获取项目属性 Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion
43 sp 设置项目属性 Set-ItemProperty -Path HKLM:\... -Name key -Value value
44 gal 获取别名 Get-Alias gal ls
45 sal 设置别名 Set-Alias ll Get-ChildItem
46 gv 获取变量 Get-Variable gv PSVersionTable
47 sv, set 设置变量 Set-Variable -Name myVar -Value "Hello"
48 clc 清空文件内容 Clear-Content file.txt
49 ac 追加内容到文件 Add-Content file.txt "New line"
50 ren, rni 重命名 Rename-Item old.txt new.txt
51 compare, diff 比较对象 Compare-Object (Get-Content file1.txt) (Get-Content file2.txt)
52 - 获取磁盘 Get-Disk
53 - 获取分区 Get-Partition -DiskNumber 0
54 - 获取卷 Get-Volume
55 gdr 获取驱动器 Get-PSDrive
56 ndr 创建驱动器映射 New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\share
57 sleep 暂停 Start-Sleep -Seconds 5 sleep 2
58 - 获取日期时间 Get-Date Get-Date -Format "yyyy-MM-dd"
59 - 获取随机数 Get-Random -Minimum 1 -Maximum 100
60 rvpa 解析路径 Resolve-Path .\*
61 - 拆分路径 Split-Path C:\folder\file.txt -Leaf
62 - 连接路径 Join-Path C:\folder file.txt
63 ft 表格格式化 `Get-Process
64 fl 列表格式化 `Get-Process
65 ogv 网格视图 `Get-Process
66 - 启动后台作业 Start-Job -ScriptBlock { Get-Process }
67 gjb 获取作业 Get-Job
68 rcjb 接收作业结果 Receive-Job -Id 1
69 rjb 删除作业 Remove-Job -Id 1
70 - 获取执行策略 Get-ExecutionPolicy
71 - 设置执行策略 Set-ExecutionPolicy RemoteSigned
72 gmo 获取模块 Get-Module -ListAvailable
73 ipmo 导入模块 Import-Module ActiveDirectory
74 gwmi WMI查询(已弃用) Get-WmiObject Win32_ComputerSystem
75 gcim CIM查询(推荐) Get-CimInstance Win32_OperatingSystem

PowerShell 特有功能

功能 描述 示例
管道对象 传递对象而非文本 `Get-Process
对象导航 使用点符号访问属性 $proc = Get-Process notepad; $proc.CPU
数组操作 强大的数组功能 $arr = 1..10; $arr[0]; $arr[-1]; $arr[2..5]
哈希表 键值对集合 $hash = @{Name="John"; Age=30}; $hash.Name
正则表达式 内置正则支持 "test123" -match "\d+"
字符串插值 变量嵌入字符串 $name = "World"; "Hello $name"
Splatting 参数展开 $params = @{Path="C:\"; Recurse=$true}; Get-ChildItem @params
脚本块 可执行代码块 $sb = { Get-Date }; & $sb
远程执行 远程命令执行 Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }
模块系统 可扩展模块 Install-Module -Name Az

命令对照表

文件和目录操作

任务 CMD PowerShell
列出文件 dir Get-ChildItem / ls / dir
切换目录 cd path Set-Location path / cd path
创建目录 mkdir folder New-Item -ItemType Directory folder / mkdir folder
删除文件 del file.txt Remove-Item file.txt / rm file.txt
删除目录 rmdir /s folder Remove-Item -Recurse folder / rm -r folder
复制文件 copy src dst Copy-Item src dst / cp src dst
移动文件 move src dst Move-Item src dst / mv src dst
重命名文件 ren old new Rename-Item old new / ren old new
查看文件 type file.txt Get-Content file.txt / cat file.txt
写入文件 echo text > file Set-Content file.txt "text" / "text" > file
追加文件 echo text >> file Add-Content file.txt "text" / "text" >> file

进程管理

任务 CMD PowerShell
列出进程 tasklist Get-Process / ps
结束进程(按名称) taskkill /IM name.exe /F Stop-Process -Name name / kill name
结束进程(按ID) taskkill /PID 1234 /F Stop-Process -Id 1234 / kill 1234
启动进程 start program.exe Start-Process program.exe / start program.exe
查找特定进程 `tasklist find "chrome"`

网络操作

任务 CMD PowerShell
显示IP配置 ipconfig Get-NetIPAddress / ipconfig
刷新DNS ipconfig /flushdns Clear-DnsClientCache
Ping测试 ping google.com Test-NetConnection google.com / ping google.com
端口测试 telnet host port Test-NetConnection host -Port port
显示连接 netstat -an Get-NetTCPConnection / netstat -an
DNS查询 nslookup domain.com Resolve-DnsName domain.com / nslookup domain.com
路由跟踪 tracert google.com Test-NetConnection google.com -TraceRoute
HTTP请求 curl url (需安装) Invoke-WebRequest url / curl url

系统信息

任务 CMD PowerShell
系统信息 systeminfo Get-ComputerInfo / systeminfo
当前用户 whoami $env:USERNAME / whoami
计算机名 hostname $env:COMPUTERNAME / hostname
环境变量 set Get-ChildItem Env: / $env:PATH
Windows版本 ver $PSVersionTable.PSVersion / ver
当前目录 cd Get-Location / pwd

服务管理

任务 CMD PowerShell
列出服务 sc query / net start Get-Service
启动服务 sc start name / net start name Start-Service name
停止服务 sc stop name / net stop name Stop-Service name
重启服务 - Restart-Service name
服务状态 sc query name Get-Service name

文本搜索和处理

任务 CMD PowerShell
搜索文本 find "text" file Select-String "text" file / sls "text" file
正则搜索 findstr /r "pattern" file Select-String -Pattern "pattern" file
递归搜索 findstr /s "text" *.txt `Get-ChildItem -Recurse
排序 sort file.txt `Get-Content file.txt
去重 `sort file.txt uniq`

其他常用操作

任务 CMD PowerShell
清屏 cls Clear-Host / cls
历史命令 doskey /history Get-History / h
别名 doskey ll=dir /w Set-Alias ll Get-ChildItem
帮助 command /? Get-Help command / help command
暂停 pause Read-Host "Press Enter" / pause
延迟 timeout /t 5 Start-Sleep -Seconds 5 / sleep 5

快捷键参考

CMD 快捷键

快捷键 功能
Tab 文件名/路径自动补全
/ 浏览命令历史
F1 逐字符重复上一命令
F3 重复上一命令
F7 命令历史列表(窗口)
F8 搜索命令历史(前缀匹配)
F9 按编号选择历史命令
Alt+F7 清除命令历史
Ctrl+C 终止当前命令
Ctrl+V 粘贴
Ctrl+M 进入/退出标记模式

PowerShell 快捷键

快捷键 功能
Tab 智能补全(参数、cmdlet、文件)
Ctrl+Space 显示所有可能的补全
/ 浏览命令历史
Ctrl+R 反向搜索历史
Ctrl+S 正向搜索历史
F7 命令历史列表
F8 搜索命令历史(前缀匹配)
Ctrl+C 终止当前命令
Ctrl+V 粘贴
Ctrl+L 清屏(等同于 cls
Ctrl+Home 删除从光标到行首
Ctrl+End 删除从光标到行尾
Alt+F7 清除命令历史
PgUp 显示第一条命令
PgDn 显示最后一条命令

PowerShell 高级用法示例

1. 管道和过滤

powershell 复制代码
# 查找CPU占用高的进程
Get-Process | Where-Object CPU -gt 10 | Sort-Object CPU -Descending | Select-Object -First 5

# 查找大文件
Get-ChildItem -Recurse | Where-Object Length -gt 100MB | Sort-Object Length -Descending

# 统计文件类型
Get-ChildItem -Recurse | Group-Object Extension | Sort-Object Count -Descending

2. 远程管理

powershell 复制代码
# 远程执行命令
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service }

# 建立远程会话
$session = New-PSSession -ComputerName Server01
Invoke-Command -Session $session -ScriptBlock { Get-Process }
Remove-PSSession $session

3. 数据导出

powershell 复制代码
# 导出到CSV
Get-Process | Export-Csv processes.csv -NoTypeInformation

# 导出到JSON
Get-Service | ConvertTo-Json | Out-File services.json

# 导出到HTML
Get-Process | ConvertTo-Html | Out-File report.html

4. 批量操作

powershell 复制代码
# 批量重命名(添加前缀)
Get-ChildItem *.txt | Rename-Item -NewName { "backup_" + $_.Name }

# 批量复制并修改扩展名
Get-ChildItem *.txt | ForEach-Object { Copy-Item $_.FullName ($_.BaseName + ".bak") }

# 批量创建文件夹
1..10 | ForEach-Object { New-Item -ItemType Directory "Folder$_" }

5. 系统管理

powershell 复制代码
# 获取系统启动时间
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime

# 获取磁盘空间
Get-PSDrive -PSProvider FileSystem | Select-Object Name, Used, Free

# 获取安装的软件
Get-Package | Select-Object Name, Version

# 获取热修复
Get-HotFix | Sort-Object InstalledOn -Descending

CMD 批处理示例

1. 基础脚本结构

batch 复制代码
@echo off
setlocal enabledelayedexpansion

rem 这是注释
echo 开始执行脚本...

rem 你的命令

endlocal
pause

2. 条件判断

batch 复制代码
if exist "file.txt" (
    echo 文件存在
) else (
    echo 文件不存在
)

if "%1"=="" (
    echo 请提供参数
    exit /b 1
)

3. 循环

batch 复制代码
rem 遍历文件
for %%f in (*.txt) do (
    echo 处理: %%f
)

rem 遍历目录
for /d %%d in (*) do (
    echo 目录: %%d
)

rem 递归遍历
for /r %%f in (*.log) do (
    del "%%f"
)

4. 函数(标签)

batch 复制代码
call :myFunction "参数1" "参数2"
goto :end

:myFunction
echo 参数1: %~1
echo 参数2: %~2
exit /b 0

:end

执行策略说明(PowerShell)

PowerShell 脚本执行策略:

策略 描述
Restricted 默认设置,不允许运行脚本
AllSigned 只能运行经过签名的脚本
RemoteSigned 本地脚本可运行,远程脚本需签名(推荐)
Unrestricted 所有脚本都可运行(有警告)
Bypass 无限制,无警告
powershell 复制代码
# 查看当前策略
Get-ExecutionPolicy

# 设置策略(需管理员权限)
Set-ExecutionPolicy RemoteSigned

# 临时绕过策略运行脚本
powershell -ExecutionPolicy Bypass -File script.ps1

附录:特殊变量

CMD 环境变量

变量 描述
%CD% 当前目录
%DATE% 当前日期
%TIME% 当前时间
%RANDOM% 随机数 (0-32767)
%ERRORLEVEL% 上一命令退出代码
%COMPUTERNAME% 计算机名
%USERNAME% 当前用户名
%USERPROFILE% 用户主目录
%PATH% 系统路径
%TEMP% / %TMP% 临时文件夹
%PROGRAMFILES% Program Files路径
%WINDIR% Windows目录

PowerShell 自动变量

变量 描述
$PSVersionTable PowerShell版本信息
$HOME 用户主目录
$PWD 当前工作目录
$env:PATH 环境变量PATH
$env:COMPUTERNAME 计算机名
$env:USERNAME 当前用户名
$_ / $PSItem 当前管道对象
$? 上一命令成功/失败
$LASTEXITCODE 上一命令退出代码
$null 空值
$true / $false 布尔值
$args 参数数组
相关推荐
素素.陈3 小时前
调用大模型解析图片中的文字
linux·windows·microsoft
羌俊恩3 小时前
win10系统升级失败,报错:0x800f0805 你的设备中缺少重要的安全和质量修复
windows·系统升级·0x800f0805·sfc·dism
!chen3 小时前
在Windows 10中获取TrustedInstaller权限的方法(附具体操作步骤)
windows
慕容雪_3 小时前
springboot jar注册windows服务(nssm版)
windows·spring boot·jar·nssm·注册服务
私人珍藏库3 小时前
[Windows] MusicPlayer2 V2.78
windows·音视频·工具·软件·win·music
魂万劫14 小时前
如何在虚拟机VM上|Linux环境内安装windows
linux·运维·服务器·windows
fengye20716115 小时前
win10+VM17+虚拟机win10搭建(三)
windows
Irene199116 小时前
Bash、PowerShell 常见操作总结
bash·powershell
love530love16 小时前
Windows 11 下再次成功本地编译 Flash-Attention 2.8.3 并生成自定义 Wheel(RTX 3090 sm_86 专属版)
人工智能·windows·笔记·编译·flash_attn·flash-attn·flash-attention