用 doskey 创建 cmd.exe 命令别名

doskey 宏相当于 cmd.exe 版的 bash 别名——给长命令起短名字,并用 $* 传递参数。

Windows命令提示符别名命令行低风险
运行 doskey 名称=命令 $* 定义宏;$* 会把后续参数原样传给命令。

01命令

命令
$ doskey ll=dir /od $* ; doskey gs=git status $*

02作用说明

cmd.exe 没有原生的 alias 命令,doskey 宏正好补位,还支持参数替换:$1-$9 获取单个参数,$* 传递整个尾部参数。所有人都容易忽略的一点:宏只存在于创建它的那个 cmd.exe 进程中,窗口一关就消失。要持久化,可用 doskey /macros > macros.cmd 保存,再通过 AutoRun 注册表值(HKCU\Software\Microsoft\Command Processor)配合 /macrofile 加载。

03风险

风险低风险
使用条件Macros exist only for the current cmd.exe session; load them persistently via an AutoRun registry value pointing to a macro file
!注意: 除非通过 AutoRun 从宏文件加载,否则 doskey 宏会随控制台会话一起消失。

04撤销

撤销
$ doskey ll= ; doskey gs=

05来源

  • Doskey - Create macros
    SS64 · technical · 2026-09-17
    依据: Explains macro scoping: macros are per-console and can be made persistent through AutoRun registry entries or /macrofile.
  • doskey (Windows Commands)
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: Documents doskey macro syntax, the $* parameter for trailing arguments, and /macrofile for loading saved macros.

最后验证: 2026-09-17 · 分类: 开发者

06相关方案