Windows CMD 命令大全 🪟

⚡ 掌握 Windows 命令行,提升系统管理效率的终极指南
📑 目录导航
📁 文件与目录管理
📋 目录操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| :: 显示当前目录内容 dir dir /w :: 宽列表格式 dir /s :: 包含子目录 dir /ah :: 显示隐藏文件 dir /q :: 显示文件所有者
:: 切换目录 cd C:\Users cd /d D:\Projects :: 切换驱动器 cd .. :: 上级目录 cd \ :: 根目录
:: 创建和删除目录 mkdir NewFolder mkdir "Folder with Spaces" rmdir EmptyFolder rmdir /s /q FolderWithContent :: 强制删除
|
📄 文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| :: 复制文件 copy source.txt destination.txt copy *.txt D:\Backup\ :: 复制所有txt文件 copy file1.txt + file2.txt combined.txt :: 合并文件
:: 移动和重命名 move oldfile.txt newfile.txt move "old name.txt" "new name.txt" move *.log C:\Logs\
:: 删除文件 del file.txt del *.tmp :: 删除所有tmp文件 del /f read-only.txt :: 强制删除只读文件 del /s /q *.log :: 递归静默删除日志
|
🌐 网络相关命令
📡 网络诊断
1 2 3 4 5 6 7 8 9 10 11 12 13
| :: 测试网络连通性 ping google.com ping -t 192.168.1.1 :: 持续ping ping -n 10 8.8.8.8 :: 指定次数 ping -l 1024 target.com :: 指定数据包大小
:: 路由追踪 tracert google.com tracert -d target.com :: 不解析主机名 tracert -h 30 target.com :: 最大跳数30
:: 路径诊断 pathping google.com :: 更详细的路由分析
|
🔧 网络配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| :: IP配置信息 ipconfig ipconfig /all :: 详细信息 ipconfig /release :: 释放IP ipconfig /renew :: 续订IP ipconfig /flushdns :: 清除DNS缓存 ipconfig /displaydns :: 显示DNS缓存
:: 网络连接状态 netstat -a :: 所有连接 netstat -an :: 所有连接(数字格式) netstat -b :: 显示进程名 netstat -o :: 显示进程ID netstat -s :: 显示统计信息
|
🔍 DNS 查询
1 2 3 4 5 6 7 8
| :: DNS查询 nslookup google.com nslookup -type=mx example.com :: 查询MX记录 nslookup -debug example.com :: 调试模式
:: 清除并注册DNS ipconfig /flushdns ipconfig /registerdns
|
⚙️ 系统信息与管理
📊 系统信息
1 2 3 4 5 6 7 8 9 10 11 12
| :: 详细系统信息 systeminfo systeminfo /s remote-pc :: 远程计算机信息
:: 环境变量 set :: 显示所有变量 set PATH :: 显示PATH变量 set MY_VAR=value :: 设置临时变量
:: 系统版本 ver wmic os get caption,version,buildnumber :: 详细版本信息
|
🎯 进程管理
1 2 3 4 5 6 7 8 9 10 11
| :: 进程列表 tasklist tasklist /v :: 详细信息 tasklist /fi "IMAGENAME eq chrome.exe" :: 过滤 tasklist /fo csv :: CSV格式输出 tasklist /s remote-pc :: 远程计算机
:: 进程管理 taskkill /im notepad.exe :: 按映像名结束 taskkill /pid 1234 /f :: 强制结束指定PID taskkill /fi "STATUS eq NOT RESPONDING" :: 结束无响应进程
|
🛠️ 系统维护
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| :: 磁盘检查 chkdsk C: chkdsk /f :: 修复错误 chkdsk /r :: 查找坏扇区 chkdsk /scan :: 在线扫描
:: 系统文件检查 sfc /scannow :: 扫描并修复 sfc /verifyonly :: 仅扫描 sfc /scanfile=file.txt :: 扫描指定文件
:: 驱动程序查询 driverquery driverquery /v :: 详细信息 driverquery /fo csv :: CSV格式
|
💾 磁盘管理
🎛️ 磁盘信息
1 2 3 4 5 6 7 8 9 10 11 12
| :: 磁盘信息 vol C: label C: SystemDrive :: 设置卷标 wmic logicaldisk get name,size,freespace :: 所有磁盘信息
:: 磁盘分区 diskpart :: 在diskpart中: list disk :: 列出磁盘 select disk 0 :: 选择磁盘 list partition :: 列出分区 create partition primary size=10000 :: 创建分区
|
💽 格式化管理
1 2 3 4 5 6 7
| :: 格式化磁盘 format D: /fs:ntfs :: NTFS格式 format /q :: 快速格式化 format /v:MyDrive :: 设置卷标
:: 磁盘复制 diskcopy A: B: :: 复制磁盘(需要软驱)
|
🔍 文件搜索与处理
🔎 文件搜索
1 2 3 4 5 6 7 8 9
| :: 查找文件 dir /s *.txt :: 递归搜索txt文件 dir /s /b "filename.txt" :: 简洁格式
:: 查找空目录 dir /ad /s | findstr /e " 0 File(s)"
:: 查找大文件 forfiles /s /m * /c "cmd /c if @fsize gtr 10000000 echo @path @fsize"
|
📝 内容搜索
1 2 3 4 5 6 7 8 9 10
| :: 文本搜索 find "error" log.txt find /i "warning" *.log :: 不区分大小写 find /c "success" file.txt :: 统计出现次数
:: 高级搜索 findstr /s "error" *.log :: 递归搜索 findstr /i "warning" *.txt :: 忽略大小写 findstr /n "keyword" file.txt :: 显示行号 findstr /r "^[0-9]" file.txt :: 正则表达式
|
⚡ 系统控制
⏻ 关机与重启
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| :: 关机命令 shutdown /s :: 关机 shutdown /s /t 0 :: 立即关机 shutdown /s /t 60 :: 60秒后关机 shutdown /s /f :: 强制关闭应用
:: 重启命令 shutdown /r :: 重启 shutdown /r /t 0 :: 立即重启 shutdown /r /f :: 强制重启
:: 取消关机 shutdown /a :: 取消计划关机
:: 注销和休眠 shutdown /l :: 注销 shutdown /h :: 休眠
|
⚙️ 服务管理
1 2 3 4 5 6 7 8 9
| :: 服务管理 net start :: 显示运行服务 net stop "Service Name" :: 停止服务 net start "Service Name" :: 启动服务
:: 服务详细信息 sc query :: 查询服务状态 sc stop ServiceName :: 停止服务 sc start ServiceName :: 启动服务
|
📜 批处理脚本
🎯 基本脚本命令
1 2 3 4 5 6 7
| @echo off :: 关闭命令回显 echo Hello World! :: 输出文本 pause :: 暂停等待按键 exit :: 退出脚本 cls :: 清屏 title My Script :: 设置窗口标题 color 0a :: 设置颜色(背景0,文字a)
|
🔄 流程控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| :: 条件判断 if exist file.txt ( echo File exists ) else ( echo File not found )
:: 循环处理 for %%i in (*.txt) do echo Processing %%i for /l %%i in (1,1,10) do echo Number %%i
:: 用户输入 set /p name=Please enter your name: echo Hello, %name%!
:: 调用其他脚本 call other_script.bat
|
💾 变量操作
1 2 3 4 5 6 7 8 9 10 11 12 13
| :: 变量设置和使用 set myvar=Hello echo %myvar%
:: 环境变量 echo %PATH% echo %USERNAME% echo %COMPUTERNAME%
:: 参数传递 echo First parameter: %1 echo Second parameter: %2 echo All parameters: %*
|
🎯 实用技巧
🚀 高效使用技巧
1 2 3 4 5 6 7 8 9
| :: 命令历史 F7 :: 查看命令历史 F8 :: 搜索历史命令 ↑/↓ :: 浏览历史命令
:: 自动补全 Tab :: 自动补全路径/文件名 Ctrl + C :: 终止当前命令 Ctrl + Break :: 终止批处理脚本
|
🎨 界面美化
1 2 3 4 5 6 7 8 9
| :: 设置控制台属性 mode con: cols=100 lines=40 :: 设置窗口大小 prompt $p$g :: 显示路径和> prompt $t$g :: 显示时间和> prompt $u@$h$g :: 显示用户@主机>
:: 颜色设置 color 0a :: 黑底绿字 color 1f :: 蓝底白字
|
🚀 高级用法
🔧 注册表操作
1 2 3 4 5 6 7 8 9 10 11
| :: 注册表查询 reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" reg query "HKLM\SYSTEM\CurrentControlSet\Services" /s
:: 注册表修改 reg add "HKCU\Software\MyApp" /v "Setting" /t REG_SZ /d "Value" reg delete "HKCU\Software\MyApp" /f
:: 导出导入 reg export "HKCU\Software\MyApp" backup.reg reg import backup.reg
|
📊 系统监控
1 2 3 4 5 6 7 8 9 10
| :: 性能监控 typeperf "\Processor(_Total)\% Processor Time" -sc 10 typeperf "\Memory\Available MBytes" -sc 5
:: 事件日志 wevtutil qe System /c:5 /rd:true /f:text wevtutil qe Application /c:5 /rd:true /f:text
:: 资源使用 systeminfo | findstr /C:"Total Physical Memory" /C:"Available Physical Memory"
|
🌐 高级网络
1 2 3 4 5 6 7 8 9 10 11
| :: 端口检测 telnet hostname port netstat -ano | findstr :8080
:: 网络重置 netsh winsock reset netsh int ip reset
:: 防火墙管理 netsh advfirewall show allprofiles netsh advfirewall set allprofiles state on
|
💡 专业提示: 熟练掌握 CMD 命令可以极大提高 Windows 系统管理效率。建议将常用命令保存为批处理脚本,并定期备份重要配置。对于生产环境操作,务必先在测试环境中验证命令效果。
