启动命令解释器的新实例,cmd.exe。 如果未使用参数, cmd
则显示作系统的版本和版权信息。
Note
Users seeking more advanced capabilities are encouraged to explore PowerShell for enhanced scripting and automation.
Syntax
cmd [/c|/k] [/s] [/q] [/d] [/a|/u] [/t:{<b><f> | <f>}] [/e:{on | off}] [/f:{on | off}] [/v:{on | off}] [<string>]
Parameters
Parameter | Description |
---|---|
/c | 执行由 <string> 命令处理器指定的命令,然后退出命令处理器。 |
/k | 执行由 <string> 命令处理器指定的命令,并使命令处理器保持运行。 |
/s | 与 /c 或 /k ,触发特殊的非分析规则,这些规则将第一个和最后一个引号(" )条带在周围 <string> ,但使命令的其余部分保持不变。 |
/q | 关闭回声。 |
/d | 禁用自动运行命令的执行。 |
/a | 将命令输出的格式设置为美国国家标准研究所(ANSI)。 |
/u | 将命令输出的格式设置为 Unicode。 |
/t:{<b><f> | <f> } |
Sets the background (b) and foreground (f) colors. |
/e:on | 启用命令扩展。 |
/e:off | 禁用命令扩展。 |
/f:on | 启用文件和目录名称完成。 |
/f:off | 禁用文件和目录名称完成。 |
/v:on | 启用延迟的环境变量扩展。 |
/v:off | 禁用延迟的环境变量扩展。 |
<string> |
指定要执行的作。 |
/? | 在命令提示符下显示帮助。 |
下表列出了可以用作值 <b>
的有效十六进制数字,以及 <f>
:
Value | Color |
---|---|
0 | Black |
1 | Blue |
2 | Green |
3 | Aqua |
4 | Red |
5 | Purple |
6 | Yellow |
7 | White |
8 | Gray |
9 | Light blue |
a | Light green |
b | Light aqua |
c | Light red |
d | Light purple |
e | Light yellow |
f | Bright white |
Remarks
若要将命令输出重定向到另一个命令的输入,请使用管道 (
|
) 运算符。 For example:<command1> | <command2>
仅当上一个命令失败时,双管道 (
||
) 运算符才用于执行下一个命令。 在给定方案中,command2
仅当失败时才command1
执行。 For example:<command1> || <command2>
若要将命令输出重定向到文件,请使用大于尖括号
>
字符。 For example:<command1> > <file1.txt>
若要将多个命令组合在一起或嵌套在一起,请使用
&
。 For example:<command1> & <command2>
若要对多个命令
<string>
使用,请用命令分隔符分隔&&
它们。 For example:<command1> && <command2> && <command3>
Note
使用
&&
时,仅当上述命令成功完成时,以下命令&&
才会运行。Cmd
执行第一个命令,仅在上一个命令成功时继续执行下一个命令。 否则,如果上一个命令失败,则不会执行后续命令。与
&
、管道|
和括号( )
是特殊字符,在将转义字符或引号作为参数传递时,这些字符前面必须带有转义符^
或引号。如果命令成功完成作,它将返回零(0)或无退出代码的退出代码。
如果提供的目录路径、文件或任何信息包含空格,则必须在文本周围使用双引号(
" "
如"Computer Name"
。 For example:mkdir Test && mkdir "Test 2" && move "Test 2" Test
必须在以下特殊字符周围使用引号: & <> [ ] |{ } ^ = ; ! ' + , ' ~ [空格]。
如果指定
/c
或/k
指定cmd
进程,则仅当满足以下所有条件时,才会保留余<string>
下和引号:- 你也不会使用
/s
。 - 只使用一组引号。
- 您不使用引号中的任何特殊字符,例如: & ( <> ) @ ^ |。
- 在引号中使用一个或多个空格字符。
- 内
<string>
引号是可执行文件的名称。
如果未满足上述条件,
<string>
则通过检查第一个字符来验证它是否为左引号来处理。 如果第一个字符是左引号,则会将其与右引号一起剥离。 保留右引号后面的任何文本。- 你也不会使用
如果未指定
/d
,cmd
请查找以下注册表子项:HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun\REG_SZ
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun\REG_EXPAND_SZ
如果存在一个或两个注册表子项,则会在所有其他变量之前执行它们。
可以使用此方法禁用特定进程的
/e:off
命令扩展。 You can enable or disable extensions for allcmd
command-line options on a computer or user session by setting the following REG_DWORD values:HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions\REG_DWORD
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions\REG_DWORD
Set the REG_DWORD value to either 0×1 (enabled) or 0×0 (disabled) in the registry by using Regedit.exe. 用户指定的设置优先于计算机设置,命令行选项优先于注册表设置。
Caution
注册表编辑不当可能会严重损坏系统。 在更改注册表之前,应备份计算机上任何有价值的数据。
启用命令扩展时,以下命令将受到影响:
assoc
call
-
chdir
(cd
) color
-
del
(erase
) endlocal
for
ftype
goto
if
-
mkdir
(md
) popd
prompt
pushd
set
setlocal
shift
-
start
(还包括对外部命令进程的更改)
如果启用延迟环境变量扩展,则可以使用感叹号字符 (
!
) 替换运行时环境变量的值。默认情况下,不会启用文件和目录名称完成。 可以使用 启用或禁用命令
cmd
的特定进程的/f:{on | off}
文件名完成。You can enable or disable file and directory name completion for all processes of the
cmd
command on a computer or for a user logon session by setting the following REG_DWORD values:HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD
To set the REG_DWORD value, run Regedit.exe and use the hexadecimal value of a control character for a particular function (for example, 0×9 is TAB and 0×08 is BACKSPACE). Use the [0×20] value for white space, because it isn't a valid control character. 用户指定的设置优先于计算机设置,命令行选项优先于注册表设置。
Caution
注册表编辑不当可能会严重损坏系统。 在更改注册表之前,应备份计算机上任何有价值的数据。
If you enable file and directory name completion by using
/f:on
, use CTRL+D for directory name completion and CTRL+F for file name completion. The only difference between the key combinations CTRL+D and CTRL+F is that CTRL+D only matches directory names and CTRL+F matches both file and directory names. 如果在内置目录命令cd
上使用文件和目录名称完成,md
或rd
假定目录完成。Pressing CTRL+D or CTRL+F processes the file and directory name completion. 这些组合键函数将通配符追加到
<string>
不存在时,请生成匹配的路径列表,然后显示第一个匹配路径。 如果没有任何路径匹配,则文件和目录名称完成函数会发出蜂鸣声,并且不会更改显示。- To move through the list of matching paths, press CTRL+D or CTRL+F repeatedly.
- To move through the list backwards, press the SHIFT key and CTRL+D or CTRL+F simultaneously.
- To discard the saved list of matching paths and generate a new list, edit
<string>
and press CTRL+D or CTRL+F. - If you switch between CTRL+D and CTRL+F, the saved list of matching paths is discarded, and a new list is generated.
如果在匹配路径周围放置引号,则文件和目录名称完成正确处理包含空格或特殊字符的文件名。
If you process file and directory name completion from within
<string>
, at the point in<string>
where the completion was processed, any part of the path to the right of the cursor is discarded.
使用环境变量
cmd
命令行管理程序环境由确定命令行界面和作系统行为的变量定义。 可以使用两种类型的环境变量(系统和本地)来定义 command-shell 环境或整个作系统环境的行为。 系统环境变量定义全局作系统环境的行为。 本地环境变量定义当前实例 cmd
的环境行为。
只有具有管理权限的用户才能更改系统变量。 这些变量最常用于登录脚本。 仅当创建它们的用户登录到计算机时,本地环境变量才可用。 Local variables set in the HKEY_CURRENT_USER hive are valid only for the current user, but define the behavior of the global operating system environment.
以下列表按优先级降序描述各种类型的变量:
- 内置系统变量。
- System variables found in the HKEY_LOCAL_MACHINE hive.
- Local variables found in the HKEY_CURRENT_USER hive.
- Autoexec.bat 文件中设置的所有环境变量和路径。
- 登录脚本中设置的所有环境变量和路径(如果存在)。
- 在脚本或批处理文件中以交互方式使用的变量。
在命令行界面中,每个实例 cmd
继承其父应用程序的环境。 因此,可以更改新 cmd
环境中的变量,而不会影响父应用程序的环境。
可以通过运行以下命令之一来查看环境中的环境变量:
set
Get-ChildItem Env:
设置环境变量
若要查看变量,请键入:
set <VariableName>
若要添加变量,请键入:
set <VariableName>=<value>
若要删除变量,请键入:
set <VariableName>=
可以将大多数字符用作变量值,包括空格。 If you use the special characters <, >, |, &, or ^, you must precede them with the escape character ^
or double quotation marks. 如果使用双引号,它们将作为值的一部分包含在内,因为等号后面的所有内容都被视为值。 请考虑以下示例:
若要创建变量值 New&Name
,请键入:
set <VariableName>=New^&Name
若要创建变量值 "New&Name"
,请键入:
set <VariableName>="New&Name"
如果在命令提示符处键入 set <VariableName>=New&Name
(不含克拉 ^
或双引号),可能会显示以下错误消息:
"'Name' is not recognized as an internal or external command, operable program or batch file."
变量名称不区分大小写。 但是, set
在键入变量时显示该变量。 You can combine uppercase and lowercase letters in your variable names to make your code more readable, for example, UserName.
Note
- 单个环境变量的最大大小为 8,192 字节。
- 进程的所有环境变量(包括名称、值和等号)的最大总大小为 65,536 个字符。
替换环境变量值
若要替换命令行或脚本中的变量值,请将变量名称括在百分号(%VariableName%) 中。 这允许 cmd
引用变量的值,而不是将其视为文本文本。 运行脚本时, cmd
将变量的实例替换为其值。 例如,在脚本中使用 %USERNAME% 会自动替换实际用户名。 变量替换不是递归的; cmd
仅检查变量一次。