Windows command commands syntax

Windows commands

All supported versions of Windows (server and client) have a set of Win32 console commands built in.

This set of documentation describes the Windows Commands you can use to automate tasks by using scripts or scripting tools.

Prerequisites

The information that is contained in this topic applies to:

  • Windows Server 2019
  • Windows Server (Semi-Annual Channel)
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2012
  • Windows Server 2008 R2
  • Windows Server 2008
  • Windows 10
  • Windows 8.1

Command shell overview

The Command shell was the first shell built into Windows to automate routine tasks, like user account management or nightly backups, with batch (.bat) files. With Windows Script Host you could run more sophisticated scripts in the Command shell. For more information, see cscript or wscript. You can perform operations more efficiently by using scripts than you can by using the user interface. Scripts accept all Commands that are available at the command line.

Windows has two command shells: The Command shell and PowerShell. Each shell is a software program that provides direct communication between you and the operating system or application, providing an environment to automate IT operations.

PowerShell was designed to extend the capabilities of the Command shell to run PowerShell commands called cmdlets. Cmdlets are similar to Windows Commands but provide a more extensible scripting language. You can run Windows Commands and PowerShell cmdlets in Powershell, but the Command shell can only run Windows Commands and not PowerShell cmdlets.

For the most robust, up-to-date Windows automation, we recommend using PowerShell instead of Windows Commands or Windows Script Host for Windows automation.

You can also download and install PowerShell Core, the open source version of PowerShell.

Incorrectly editing the registry may severely damage your system. Before making the following changes to the registry, you should back up any valued data on the computer.

To enable or disable file and directory name completion in the Command shell on a computer or user logon session, run regedit.exe and set the following reg_DWOrd value:

To set the reg_DWOrd value, use the hexadecimal value of a control character for a particular function (for example, 0 9 is Tab and 0 08 is Backspace). User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.

Command-line reference A-Z

To find information about a specific command, in the following A-Z menu, click the letter that the command starts with, and then click the command name.

Windows command commands syntax

Starts a new instance of the command interpreter, Cmd.exe. If used without parameters, cmd displays the version and copyright information of the operating system.

Syntax

Parameters

Parameter Description
/c Carries out the command specified by string and then stops.
/k Carries out the command specified by string and continues.
/s Modifies the treatment of string after /c or /k.
/q Turns the echo off.
/d Disables execution of AutoRun commands.
/a Formats internal command output to a pipe or a file as American National Standards Institute (ANSI).
/u Formats internal command output to a pipe or a file as Unicode.
/t: < | > Sets the background (b) and foreground (f) colors.
/e:on Enables command extensions.
/e:off Disables commands extensions.
/f:on Enables file and directory name completion.
/f:off Disables file and directory name completion.
/v:on Enables delayed environment variable expansion.
/v:off Disables delayed environment variable expansion.
Specifies the command you want to carry out.
/? Displays help at the command prompt.

The following table lists valid hexadecimal digits that you can use as the values for and :

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

To use multiple commands for , separate them by the command separator && and enclose them in quotation marks. For example:

If you specify /c or /k, cmd processes, the remainder of string, and the quotation marks are preserved only if all of the following conditions are met:

You don’t also use /s.

You use exactly one set of quotation marks.

You don’t use any special characters within the quotation marks (for example: & ( ) @ ^ | ).

You use one or more white-space characters within the quotation marks.

The string within quotation marks is the name of an executable file.

If the previous conditions aren’t met, string is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.

If you don’t specify /d in string, Cmd.exe looks for the following registry subkeys:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun\REG_SZ

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun\REG_EXPAND_SZ

If one or both registry subkeys are present, they’re executed before all other variables.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

You can disable command extensions for a particular process by using /e:off. You can enable or disable extensions for all cmd 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. User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

When you enable command extensions, the following commands are affected:

assoc

call

chdir (cd)

color

del (erase)

endlocal

for

ftype

goto

if

mkdir (md)

popd

prompt

pushd

set

setlocal

shift

start (also includes changes to external command processes)

If you enable delayed environment variable expansion, you can use the exclamation point character to substitute the value of an environment variable at run time.

File and directory name completion is not enabled by default. You can enable or disable file name completion for a particular process of the cmd command with /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). User-specified settings take precedence over computer settings, and command-line options take precedence over registry settings.

Incorrectly editing the registry may severely damage your system. Before making changes to the registry, you should back up any valued data on the computer.

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. To disable a particular completion character in the registry, use the value for white space [0Г—20] because it is not a valid control character.

Pressing CTRL+D or CTRL+F, processes the file and directory name completion. These key combination functions append a wildcard character to string (if one is not present), builds a list of paths that match, and then displays the first matching path.

If none of the paths match, the file and directory name completion function beeps and does not change the display. 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. 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. If you use file and directory name completion on any of the built-in directory commands (that is, CD, MD, or RD), directory completion is assumed.

File and directory name completion correctly processes file names that contain white space or special characters if you place quotation marks around the matching path.

You must use quotation marks around the following special characters: & [ ] | < >^ = ; ! ‘ + , `

If the information that you supply contains spaces, you must use quotation marks around the text (for example, «Computer Name»).

If you process file and directory name completion from within string, any part of the path to the right of the cursor is discarded (at the point in string where the completion was processed).

How to Read Command Syntax

Learn how to interpret command syntax with these examples

The syntax of a command is basically the rules for running the command. You need to know how to read syntax notation when learning how to use a command so you can execute it properly.

As you’ve probably seen here on Lifewire and maybe other websites, Command Prompt commands, DOS commands, and even many run commands are described with all sorts of slashes, brackets, italics, etc. Once you know what all of those marks refer to, you can look at any command’s syntax and know right away what options are required and what options can be used with what other options.

Depending on the source, you might see slightly different syntax when used to describe commands. We use a method that Microsoft has historically used, and all command syntax that we’ve ever seen on any site is extremely similar, but remember that you should follow the syntax key that pertains to the commands you’re reading and not assume that all websites and documentation use the exact same method.

Command Syntax Key

The following syntax key describes how each notation in a command’s syntax is to be used. Feel free to reference this as we walk through the three examples below the table.

Command Syntax Reference Table
Notation Meaning
Bold Bold items must typed exactly as they are shown, this includes any bold words, slashes, colons, etc.
Italic Italic items are items that you must supply. Do not take an italic item literally and use it in the command as shown.
S p a c e s All spaces should be taken literally. If a command’s syntax has space, use that space when executing the command.
[Text inside brackets] Any items inside a bracket are optional. Brackets are not to be taken literally so don’t use them when executing a command.
Text outside brackets Any text not contained in a bracket is required. In the syntax of many commands, the only text not surrounded by one or more brackets is the command name itself.
The items within a brace are options, of which you must choose only one. Braces are not to be taken literally so don’t use them when executing a command.
Vertical | bar Vertical bars are used to separate items within brackets and braces. Do not take vertical bars literally—do not use them when executing commands.
Ellipsis . An ellipsis means that an item can be repeated indefinitely. Do not type ellipsis literally when executing a command and take care to use spaces and other required items as shown when repeating items.

Brackets are also sometimes referred to as square brackets, braces are sometimes referred to as squiggly brackets or flower brackets, and vertical bars are sometimes called pipes, vertical lines, or vertical slashes. Regardless of what you call them, none should ever be taken literally when executing a command.

Example #1: Vol Command

Here’s the syntax for the vol command, a command available from the Command Prompt in all versions of the Windows operating system:

vol [drive:]

The word vol is in bold, meaning that it should be taken literally. It’s also outside any brackets, meaning it’s required. We’ll take a look at brackets a few paragraphs down.

Following vol is a space. Spaces in a command’s syntax are to be taken literally, so when you’re executing the vol command, you will need to put a space between vol and anything that might come next.

Brackets indicate that whatever is contained inside them is optional—whatever is in there is not required for the command to function but might be something you want to use, depending on what you’re using the command for. Brackets are never to be taken literally so never include them when executing a command.

Inside the brackets is the italicized word drive, followed by a colon in bold. Anything italicized is something you must supply, not take literally. In this case, a drive is referring to a drive letter, so you’ll want to supply a drive letter here. Just as with vol, since : is in bold, it should be typed as shown.

Based on all of that information, here are some valid and invalid ways to execute the vol command and why:

Valid: The vol command can be executed by itself because drive: is optional because it’s surrounded by brackets.

Invalid: This time, the optional part of the command is being used, specifying drive as d, but the colon was forgotten. Remember, we know the colon accompanies the drive because it is included in the same set of brackets and we know it should be used literally because it’s bold.

Invalid: The /p option wasn’t listed in the command syntax, so the vol command doesn’t run when using it.

Valid: In this case, the optional drive: argument was used just as intended.

Example #2: Shutdown Command

The syntax listed here is for the shutdown command and is obviously much more complex than in the vol command example above. However, building on what you already know, there’s actually very little more to learn here:

shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e] [/f] [/m \\computername] [/t xxx] [/d [p:|u:]xx:yy] [/c «comment«]

Remember that items within brackets are always optional, items outside of brackets are always required, bold items and spaces are always literal, and italicized items are to be provided by you.

The big new concept in this example is the vertical bar. Vertical bars within brackets indicate optional choices. So in the example above, you can, but don’t have to, choose to include one of the following options when executing a shutdown command: /i, /l, /s, /r, /g, /a, /p, /h, or /e. Like brackets, vertical bars exist to explain command syntax and are not to be taken literally.

The shutdown command also has a nested option in [/d [p:|u:]xx:yy]—basically, an option within an option.

Like with the vol command in the first example, here are some valid and invalid ways to use the shutdown command:

Invalid: The /r and /s options can’t be used together. These vertical bars indicate choices, of which you can choose only one.

Invalid: Using /s is perfectly fine but the use of p:0:0 is not because this option is available only with the /d option, which we forgot to use. The correct usage would have been shutdown /s /d p:0:0.

Valid: All options were used correctly this time. The /r option was not used with any other choice within its set of brackets, and the /f and /t options were used as described in the syntax.

Example #3: Net Use Command

For our final example, let’s look at the net use command, one of the net commands. Its syntax is a little messy, so we’ve abbreviated it below to make explaining it a bit easier (see the full syntax here):

net use [<devicename | *>] [\\computername\sharename [<password | *>]] [/persistent:<yes | no>] [/savecred] [/delete]

The net use command has two instances of a new notation: the brace. A brace indicates that one, and only one, of the choices, separated by one or more vertical bars, is required. This is unlike the bracket with vertical bars that indicates optional choices.

Let’s look at some valid and invalid uses of net use:

Invalid: The first set of braces mean that you can specify a devicename or use the wildcard character * — you can’t do both. Either net use e: \\server\files or net use * \\server\files would have been valid ways to execute net use in this case.

Valid: We correctly used several options in this execution of net use, including one nested option. We used the * when required to choose between it and specifying a devicename, we specified a share [source] on a server [appsvr01], and then chose to specify a <password> for that share, 1lovet0visitcanada, instead of forcing net use to prompt us for one <*>. We also decided not to allow this new shared drive to be automatically reconnected next time we start the computer [/persistent:no].

Invalid: In this example, we chose to use the optional /persistent switch but forgot to include the colon next to it and also forgot to choose between the two required options, yes or no, between the braces. Executing net use /persistent:yes would have been a valid use of net use.

Читайте также:  Не нажимается кнопка активация windows
Оцените статью